VBA code not looping

CoconutP

New Member
Joined
Aug 11, 2021
Messages
7
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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
60,057
Office Version
  1. 365
Platform
  1. Windows
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.
 
Solution

CoconutP

New Member
Joined
Aug 11, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
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!
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
60,057
Office Version
  1. 365
Platform
  1. Windows
You are welcome.
Glad I was able to help!
:)
 

Forum statistics

Threads
1,147,635
Messages
5,742,250
Members
423,717
Latest member
rubthenut

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
Top