VBA code not looping

CoconutP

New Member
Joined
Aug 11, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hello, can I ask for some help - the below code only works on one worksheet but it doesn't loop, and I think it might have to do with not qualifying the ranges. I attempted to add in With ws and End with, but I came up with all kinds of errors. Could you please help update the code?

VBA Code:
Sub Refresh()
Dim CurrentMonth As String
CurrentMonth = Sheets("Filters").Range("G6")
        
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

If ws.Name Like "A" Or ws.Name Like "B" Or ws.Name Like "C" Or ws.Name Like "D" Then
        
Columns("L:BL").Select
Selection.Columns.Ungroup
Columns("L:BL").Select
Selection.EntireColumn.Hidden = False

Set Cell = Cells.Find(CurrentMonth, , xlValues, xlWhole, , , False)
If Not Cell Is Nothing Then
  ColLetter = Chr(Asc(Split(Cell.Address, "$")(1)) + 3)
End If
'MsgBox (ColLetter)

Columns(ColLetter & ":BK").Select
Selection.Columns.Group
Range("BL1").Select
ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1

'ActiveWorkbook.RefreshAll

End If

Next ws

End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Welcome to the Board!

Note that looping through the sheet does NOT actually select/activate each sheet.
So, you will either need to qualify every range, i.e.
VBA Code:
ws.Columns(...)
ws.Cells(...)
etc
or you can simply select/activate the sheet at the beginning of your loop, i.e.
VBA Code:
For Each ws In ActiveWorkbook.Worksheets
    ws.Activate

Another thing to note. You should NOT use reserved words like "Refresh" as the name of your procedures, functions, or variables.
Doing so can cause errors and unexpected results.
Reserved words are words that are already used in VBA as function, properties, methods, etc.
 
Upvote 0
Solution
Welcome to the Board!

Note that looping through the sheet does NOT actually select/activate each sheet.
So, you will either need to qualify every range, i.e.
VBA Code:
ws.Columns(...)
ws.Cells(...)
etc
or you can simply select/activate the sheet at the beginning of your loop, i.e.
VBA Code:
For Each ws In ActiveWorkbook.Worksheets
    ws.Activate

Another thing to note. You should NOT use reserved words like "Refresh" as the name of your procedures, functions, or variables.
Doing so can cause errors and unexpected results.
Reserved words are words that are already used in VBA as function, properties, methods, etc.
Thank you so very very much!! The ws.Activate works perfectly!
 
Upvote 0
You are welcome.
Glad I was able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,361
Members
449,080
Latest member
Armadillos

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