Looped ifs through multiple sheets (Nested loop?)

shabangle

New Member
Joined
Aug 9, 2013
Messages
23
Hi All,

I'd be grateful of any help in getting a looped command working across multiple sheets.

I think I'm close to getting this, but when I run the below it only performs the column hides on the last associated worksheet - it isn't applied to all worksheets (ignoring those excluded).

Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws
LastColumn = 100

'Ignore the following worksheets
If (ws.Name <> "Notes") And (ws.Name <> "Home") And (ws.Name <> "Stage0A_SO") And (ws.Name <> "Stage0A_PO") And (ws.Name <> "Stage0A_FS") And (ws.Name <> "Stage0B_IO") And (ws.Name <> "Stage1A_T") And (ws.Name <> "Stage1A_M") And (ws.Name <> "Stage1B_S") And (ws.Name <> "Stage1C_SC") And (ws.Name <> "Stage1D_RE") And (ws.Name <> "Stage2A_WE") Then​

For j = 1 To LastColumn
If Cells(11, j) = 0 And Cells(11, j) <> "" Then Columns(j).EntireColumn.Hidden = True
Next j

For k = 1 To LastColumn
If Cells(11, k) = 0 And Cells(11, k) <> "" Then Columns(k + 1).EntireColumn.Hidden = True
Next k

End If
End With
Next ws

Thanks so much for any help,
Best Regards,
Shabangle
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Could be because you do not have periods in front of the Cells property to tie them to the worksheet inside the With statement. Try this version.

Code:
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
    With ws
    LastColumn = 100
    'Ignore the following worksheets
        If (ws.Name <> "Notes") And (ws.Name <> "Home") And (ws.Name <> "Stage0A_SO") And (ws.Name <> "Stage0A_PO") And (ws.Name <> "Stage0A_FS") And (ws.Name <> "Stage0B_IO") And (ws.Name <> "Stage1A_T") And (ws.Name <> "Stage1A_M") And (ws.Name <> "Stage1B_S") And (ws.Name <> "Stage1C_SC") And (ws.Name <> "Stage1D_RE") And (ws.Name <> "Stage2A_WE") Then
            For j = 1 To LastColumn
                If .Cells(11, j) = 0 And .Cells(11, j) <> "" Then Columns(j).EntireColumn.Hidden = True
            Next j
            For k = 1 To LastColumn
                If .Cells(11, k) = 0 And .Cells(11, k) <> "" Then Columns(k + 1).EntireColumn.Hidden = True
            Next k
        End If
    End With
Next ws
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
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