Macro to Hide Columns

Mobot99

New Member
Joined
Jul 31, 2020
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hello, I am trying to hide columns in specific worksheets all at once based on the cell value in row 1 of each of those worksheets (the cell will either say HIDE or SHOW).

So basically I want to have a Hide Columns button on Sheet1 that would Hide columns in Sheet3, Sheet4, or whichever I designate. I would also like a macro to Show All Columns. Thank you in advance.
 

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
Try this.

VBA Code:
Sub Hide_Cols()
With Sheets("Sheet1").Range("1:1")
      .Replace "Hide", True, xlWhole, , False, , False, False
      On Error Resume Next
      .SpecialCells(xlConstants, xlLogical).EntireColumn.Hidden = True
      On Error GoTo 0
   End With
End Sub
 
Upvote 0
And to show the columns try this. There's probably better code out there but I am only new at this. I have adapted this from a piece forum member Fluff wrote for me.

VBA Code:
Sub Show_Cols()
With Sheets("Sheet1").Range("1:1")
      On Error Resume Next
      .SpecialCells(xlConstants, xlLogical).EntireColumn.Hidden = False
      On Error GoTo 0
   End With
     Worksheets("Sheet1").Rows("1").Replace _
     What:="TRUE", Replacement:="Hide", _
     SearchOrder:=xlByRows, MatchCase:=True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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