VBA Code to hide rows on selected sheets based on cell value

mkpippin

New Member
Joined
Jan 26, 2012
Messages
21
Hi All,

I'm looking to add a macro that will cycle through a few specific worksheets (Sheet1, Sheet2, Sheet3) and hide rows if the value in column A of that row equals "Hide". This should seemingly be simple, but I can't quite get it to work.

Any insight would be greatly appreciated

Thanks!!
 

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,)
Why not just use Filters to do it?
If you want VBA, you can use VBA to set up the Filters. You can turn on the Macro Recorder and record yourself doing it to get much of the code you need to do this.
 
Upvote 0
Filters won't work for me in this scenario because I have many end users who will be filtering and clearing filters on this sheet. When they clear the filters, I want these rows to still be hidden
 
Upvote 0
Hi mkpippin,

Try this:

Code:
Option Explicit
Sub Macro1()

    Dim varMySheet As Variant
    Dim wsMySheet  As Worksheet
    Dim lngLastRow As Long
    Dim lngMyRow   As Long
    Dim xlnCalcMethod As XlCalculation
    
    With Application
        .ScreenUpdating = False
        xlnCalcMethod = .Calculation
        .Calculation = xlCalculationManual
    End With
    
    For Each varMySheet In Array("Sheet1", "Sheet2", "Sheet3")
        Set wsMySheet = Sheets(CStr(varMySheet))
        lngLastRow = wsMySheet.Cells(Rows.Count, "A").End(xlUp).Row
        For lngMyRow = lngLastRow To 1 Step -1
            If StrConv(wsMySheet.Range("A" & lngMyRow), vbProperCase) = "Hide" Then
                wsMySheet.Rows(lngMyRow).Hidden = True
            Else
                wsMySheet.Rows(lngMyRow).Hidden = False
            End If
        Next lngMyRow
        Set wsMySheet = Nothing
    Next varMySheet
    
    With Application
        .Calculation = xlnCalcMethod
        .ScreenUpdating = True
    End With

End Sub

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,695
Members
448,979
Latest member
DET4492

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