VBA to hide cells on multiple sheet if cell equals

whazzzzzupp17

New Member
Joined
Jul 23, 2018
Messages
21
Hello, I created a code to hide columns if the cell in the first row contains "H".

I'm having trouble modifying this code to apply it to (3) other sheets. (data1, data2, data3)

How do I create this other than clicking each sheet and re-running the macro.

VBA Code:
Sub Hide()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    Dim H As Range

     For Each H In Range("A1:Z1").Cells
        If H.Value = "H" Then
            H.EntireColumn.Hidden = True
        End If
    Next H

    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
If the only sheets in your workbook are as listed above then:
VBA Code:
Option Explicit

Sub Hide()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Dim ws As Worksheet
    Dim H As Range
    For Each ws In Worksheets
     For Each H In ws.Range("A1:Z1").Cells
        If ws.H.Value = "H" Then
            ws.H.EntireColumn.Hidden = True
        End If
    Next H
    Next ws
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,007
Messages
6,122,670
Members
449,091
Latest member
peppernaut

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