Macro to hide rows

jnewton34120

New Member
Joined
Feb 2, 2024
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I am looking for help with what I hope is an easy macro. I am looking to have it look at all rows from 3 down and if the value in columns C and D are 0 then hide the row. The list ends on row 1063. If one of the columns has a 0 but the other doesn't then it should hide it. Thank you in advance for any help!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
One way:
VBA Code:
Option Explicit
Sub Hide_Rows()
    Application.ScreenUpdating = False
    Dim i As Long
    With Range("C3:D1063")
        .EntireRow.Hidden = False
        For i = 1 To 1061
            If Len(.Cells(i, 1)) > 0 And .Cells(i, 1) = 0 Or Len(.Cells(i, 2)) > 0 And .Cells(i, 1) = 0 Then .Rows(i).Hidden = True
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks to the sharp eyes of @Alex Blakenburg for pointing out the error in my suggested code. Revised version below - please let me know if it does/doesn't achieve your aim.

Rich (BB code):
Option Explicit
Sub Hide_Rows()
    Application.ScreenUpdating = False
    Dim i As Long
    With Range("C3:D1063")
        .EntireRow.Hidden = False
        For i = 1 To 1061
            If Len(.Cells(i, 1)) > 0 And .Cells(i, 1) = 0 Or Len(.Cells(i, 2)) > 0 And .Cells(i, 2) = 0 Then .Rows(i).Hidden = True
        Next i
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
I think the logical test can be simplified to
VBA Code:
If .Cells(i, 1).Value = 0 Or .Cells(i, 2).Value = 0 Then
 
Upvote 0

Forum statistics

Threads
1,215,433
Messages
6,124,861
Members
449,195
Latest member
MoonDancer

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