Confused with VBA looping through Worksheets

cedricthecat

Active Member
Joined
May 17, 2007
Messages
460
Hi folks

I have this macro, which works fine when set to hide Worksheets (hides all sheets except "Overview")

Code:
Sub HideSheets()
Dim wsSheet As Worksheet
    For Each wsSheet In Worksheets
    If wsSheet.Name <> "Overview" Then
        wsSheet.Visible = False
       End If
     Next wsSheet
End Sub

However, I am trying to adapt this to hide Columns in all sheets except "Overview" (as below) but instead it only hides the specified Columns in "Overview"!! Can someone tell me what I am doing wrong?!

Code:
Sub HideColumns()
Dim wsSheet As Worksheet
    For Each wsSheet In Worksheets
    If wsSheet.Name <> "Overview" Then
        Columns("A:M").Select
        Selection.EntireColumn.Hidden = True
       End If
     Next wsSheet
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Code:
Sub HideColumns()
Dim wsSheet As Worksheet
    For Each wsSheet In Worksheets
    If wsSheet.Name <> "Overview" Then [COLOR="Red"]wsSheet.Columns("A:M").Hidden = True[/COLOR]
     Next wsSheet
End Sub
 
Upvote 0
Dim wsSheet As Worksheet
For Each wsSheet In Worksheets
If wsSheet.Name <> "Overview" Then

wsSheet.Activate

ActiveSheet.Columns("A:M").Select

Selection.EntireColumn.Hidden = True
End If
Next wsSheet
 
Upvote 0
Thanks Saltkev - I used Neil's solution to the original problem, but yours worked nicely on a different Workbook which had a similar, but not quite the same, issue!
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,240
Members
452,898
Latest member
Capolavoro009

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