Hide columns based on cell value

rmatenchuk

New Member
Joined
Jun 11, 2010
Messages
16
Hi,

I am struggling to create a macro for hidding columns based on various tabs with a true or false trigger.


Below is my code... what am I doing wrong?

Probably a quick one for you guys, but tearing my hair out! Also - it is likely I can slim it down, any pointers are very welcomed.

Sub hide_master()
Application.ScreenUpdating = False

Dim rng As Range
Dim mycell As Range
Set rng = Range("A5:eY5")
Dim rng2 As Range
Dim mycell2 As Range
Set rng2 = Range("A1:eY1")
Sheets("Summary").Activate
For Each mycell In rng
If mycell.Value = "False" Then
mycell.EntireColumn.Hidden = True
End If
Next
For Each mycell In rng
If mycell.Value = "true" Then
mycell.EntireColumn.Hidden = False
End If
Next


Sheets("OWNER OCC").Activate

For Each mycell2 In rng2
If mycell2.Value = "False" Then
mycell2.EntireColumn.Hidden = True
End If
Next
For Each mycell2 In rng2
If mycell2.Value = "true" Then
mycell2.EntireColumn.Hidden = False
End If


Next

Application.ScreenUpdating = True

Sheets("Summary").Activate


End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try this code:
Code:
Sub hide_master()
Application.ScreenUpdating = False

Dim wsS As Worksheet, wsOO As Worksheet
Dim iCol As Integer

Set wsS = Sheets("Summary")
Set wsOO = Sheets("OWNER OCC")

For iCol = 1 To 155 'Columns A to EY
    If wsOO.Cells(1, iCol) = "True" Then
        wsOO.Cells(1, iCol).EntireColumn.Hidden = False
    ElseIf wsOO.Cells(1, iCol) = "False" Then
        wsOO.Cells(1, iCol).EntireColumn.Hidden = True
    End If
    
    If wsS.Cells(5, iCol) = "True" Then
        wsS.Cells(5, iCol).EntireColumn.Hidden = False
    ElseIf wsS.Cells(5, iCol) = "False" Then
        wsS.Cells(5, iCol).EntireColumn.Hidden = True
    End If
Next iCol

Application.ScreenUpdating = True
wsS.Activate
End Sub
 
Upvote 0
Thanks Andrew.

The "summary" page is working great, but the "Owner Occ" page is still not updating? What am i doing wrong?

Thanks for your help!

Ryan
 
Upvote 0

Forum statistics

Threads
1,216,473
Messages
6,130,838
Members
449,597
Latest member
buikhanhsang

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