Excel VBA to hide columns based on cell value

Stuepef

Board Regular
Joined
Oct 23, 2017
Messages
128
Office Version
  1. 2021
Platform
  1. Windows
I have 3 tabs within my workbook that I would like to hide columns based on cell value. The following code hides some of the columns, but I am looking for assistance to add the rest of the columns. Here is what I am looking for:

Hide Columns J:M if cell K18 is ""
Hide Columns N:Q if cell O18 is ""
Hide Columns R:U if cell S18 is ""
Hide Columns V:Y if cell W18 is ""
Hide Columns Z:AC if cell AA18 is ""
Hide Columns AD:AG if cell AE is ""

Current code:
Code:
Sub HideColumnsSummary()    
Dim wsMySheet As Worksheet
    Application.ScreenUpdating = False
    For Each wsMySheet In ThisWorkbook.Sheets
        Select Case wsMySheet.Name
        Case Is = "Summary 1", "Summary (2)", "Summary (3)"
            With wsMySheet
                If .Range("K18").Value = "" Then
                Columns("J:M").EntireColumn.Hidden = True
                Else
                Columns("J:M").EntireColumn.Hidden = False
            End If
        End With
    End Select
    Next wsMySheet
    Application.ScreenUpdating = True
End Sub

I am sure there is a better way to write this code so I am open to any suggestions.

Thanks!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
How about
Code:
Sub HideColumnsSummary()
Dim wsMySheet As Worksheet
   Application.ScreenUpdating = False
   For Each wsMySheet In Sheets(Array("Summary 1", "Summary (2)", "Summary (3)"))
      With wsMySheet
         .Columns("J:M").EntireColumn.Hidden = .Range("K18").Value = ""
         .Columns("N:Q").Hidden = .Range("O18").Value = ""
         .Columns("R:U").Hidden = .Range("S18").Value = ""
      End With
   Next wsMySheet
   Application.ScreenUpdating = True
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
@Fluff - How would I change the code you provided to code names? The tab names update with other code I have:
Code:
For Each wsMySheet In Sheets(Array("Summary 1", "Summary (2)", "Summary (3)"))

Summary 1 -> Sheet6
Summary (2) -> Sheet7
Summary (3) -> Sheet8
 
Upvote 0
Like
Code:
   For Each wsMySheet In Sheets(Array(Sheet6.Name, Sheet7.Name, Sheet8.Name))
 
Upvote 0
@Stuepef - Thanks for having posted this Question
@Fluff - Yo ma man here we are again... :love: :love: :love: Love yo support Bro...(y)Thank you...
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,173
Members
452,615
Latest member
bogeys2birdies

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