Help with Hiding Columns Macro

Mobot99

New Member
Joined
Jul 31, 2020
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hello, need help with a macro that hides columns in multiple worksheets based on whether the drop down menu in that column in one worksheet says "HIDE"

Here is what I have so far, my issue is how do I exclude certain worksheets? Including the worksheet that the macro is in? Or I can just include certain worksheets, whichever is easier.

Sub Hide_Columns_Containing_Value_All_Sheets()

Dim c As Range
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
If Not ws.Name = "Sheet9" Then
End If
For Each c In Range("D4:N4").Cells
If c.Value = "HIDE" Then
c.EntireColumn.Hidden = True
End If
Next c
Next ws
End Sub
 
Which line gives that error?
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Not showing a particular line, here is a screenshot:
 

Attachments

  • Capture.PNG
    Capture.PNG
    56.8 KB · Views: 6
Upvote 0
Ok, Sheet9 is the code name, not the sheet name. The sheet name is Renewal Column Selection.
Try this instead
VBA Code:
Sub Hide_Columns_Containing_Value_All_Sheets()
   
   Dim c As Range
   Dim ws As Worksheet
   
   For Each ws In ActiveWorkbook.Worksheets
      Select Case ws.CodeName
         Case "Sheet9", "Sheet8", "Sheet3", "Sheet6" ' sheet names to be excluded
         Case Else
            For Each c In Sheet9.Range("D4:N4").Cells
               ws.Range(c.Address).EntireColumn.Hidden = c.Value = "HIDE"
            Next c
      End Select
   Next ws
End Sub
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,037
Members
449,205
Latest member
Eggy66

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