VBA code to hide a row does not work

L

Legacy 330376

Guest
Hi All,

I have a VBA code which does the following-
* Hides a row if the values of column J and O are less than 12

It should look at row 3 and beyond.

The VBA code is below, any ideas why it is not working?
It does not come up with an error message or anything.
The cells which it looks for, are indexed formulas so I am not sure if it is because of this.
I would like to use the code in an automatic state so probably use the private sub worksheet change function.

Code:
Sub Change()
Dim i As Long, lastRow As Long
Set wsDATA = ActiveSheet

With wsDATA
For i = 2 To lastRow
If (.Cells(i, "J") < "12" And .Cells(i, "O") < "12") Then
   .Range("A" & i).EntireRow.Hidden = True
   End If
   Next i
   End With
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
try this

Code:
Sub Change()
    Dim i As Long, lastRow As Long
    Set wsDATA = ActiveSheet

    With wsDATA
        For i = 3 To lastRow
            If .Cells(i, "J") < 12 And .Cells(i, "O") < 12 Then
                .Range("A" & i).EntireRow.Hidden = True
            End If
        Next i
    End With
End Sub
 
Upvote 0
Hi Diddi,
Still does not work.
Might need to rejig the code, what do you think?
 
Upvote 0
The variable lastrow is not initialized, so it's 0, so the code inside the loop never executes.
 
Upvote 0
As already mentioned, your LastRow variable has not been initialized.

See if this helps:

Code:
Sub Change()
    Dim i As Long, lastRow As Long
    Set wsDATA = ActiveSheet
    
    With wsDATA
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For i = 2 To lastRow
            .Cells(i, 1).EntireRow.Hidden = .Cells(i, "J").Value < 12 And .Cells(i, "O").Value < 12
        Next i
    End With
End Sub

Dave
 
Upvote 0
As already mentioned, your LastRow variable has not been initialized.

See if this helps:

Code:
Sub Change()
    Dim i As Long, lastRow As Long
    Set wsDATA = ActiveSheet
    
    With wsDATA
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        For i = 2 To lastRow
            .Cells(i, 1).EntireRow.Hidden = .Cells(i, "J").Value < 12 And .Cells(i, "O").Value < 12
        Next i
    End With
End Sub

Dave

Hi Dave,

It comes up with an error on the following line-

Code:
 .Cells(i, 1).EntireRow.Hidden .Cells(i, "J").Value < 12 And .Cells(i, "O").Value < 12

Run-Time Error '1004'
Application-defined or object-defined error.
 
Upvote 0
As published, code should work ok but note that line you have highlighted with problem has the equals ("=") sign missing

Rich (BB code):
.Cells(i, 1).EntireRow.Hidden = .Cells(i, "J").Value < 12 And .Cells(i, "O").Value < 12

Also, ensure sheet is not protected when code run.

Dave
 
Upvote 0
As published, code should work ok but note that line you have highlighted with problem has the equals ("=") sign missing

Rich (BB code):
.Cells(i, 1).EntireRow.Hidden = .Cells(i, "J").Value < 12 And .Cells(i, "O").Value < 12

Also, ensure sheet is not protected when code run.

Dave

Hi Dave,
Thank you very much- it works on a test spreadsheet however when I try it on my actual spreadsheet it does not work. This is because columns J and O are index formulas, is there a way to get round it?
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top