Excel macro that hides a given number of rows

advertisements

I was searching for answers regarding this issue, but no luck. I want an excel macro to hides rows based on a cell value. My table is between row 18 and row 418 could you please take a look at my VBA code:

Sub HideRows()

Dim np As Integer

np = Range("W1").Value
'hidding all the table rows first
ActiveSheet.Rows("18:418").EntireRow.Hidden = True
'showing only from row 18 to the number given by cell "W1"
ActiveSheet.Rows("18:np").EntireRow.Hidden = False

End Sub

It is giving me a type mismatch an error, is there any way to make this work? Thank you so much


Change

ActiveSheet.Rows("18:np").EntireRow.Hidden = False

to

ActiveSheet.Rows("18:" & np).EntireRow.Hidden = False