Home > Back-end >  Hide Row if Cell is blank
Hide Row if Cell is blank

Time:12-14

I have a worksheet active code that hides all rows if the contents of column A:A is blank. Now, this code works fine. I am trying to apply this same code to a button on the same sheet which will then hide any rows in Column M:M that is also blank.

Sub Hide_Unused_Rows()

'Hide rows with no requirements
Dim lastrow As Long
Dim datasheet As Worksheet
Dim i As Long

Set datasheet = Sheets("ORDER FORM")

lastrow = datasheet.Range("M" & datasheet.Rows.Count).End(xlUp).Row

For i = lastrow To 2 Step -1
    If datasheet.Cells(i, 1) = "" Then
        datasheet.Rows(i & ":" & i).EntireRow.Hidden = True
    End If
Next I

End Sub

As mentioned above this code works perfectly when it is applied to phase 1 (Worksheet activate) but when applied to phase 2 it doesn't hide the rows where cells in column M are blank. The code for the 2 actions is the same with the exception that lastrow = datasheet.Range("M" & datasheet.Rows.Count).End(xlUp).Row reference either Column A or Column M.

CodePudding user response:

You forgott to change the column here:

If datasheet.Cells(i, 1) = "" Then

See below.

Sub Hide_Unused_Rows()

'Hide rows with no requirements
Dim lastrow As Long
Dim datasheet As Worksheet
Dim i As Long

Set datasheet = Sheets("ORDER FORM")

lastrow = datasheet.Range("M" & datasheet.Rows.Count).End(xlUp).Row

For i = lastrow To 2 Step -1
    If datasheet.Cells(i, "M") = "" Then
        datasheet.Rows(i & ":" & i).EntireRow.Hidden = True
    End If
Next i

End Sub
  • Related