PDA

View Full Version : [SOLVED] Loop sheets that begin with given chars



Pamella
07-25-2019, 11:36 PM
I would like to loop each sheet and perform some action for those sheets that start with REDS.

I have written some code, but erroring.


Sub LoopSheets()

Dim mWB As Workbook
Dim mWS As Worksheet
Dim arySheets
Dim sFile
Dim iSheet As Long


With ActiveWorkbook


'If sFile <> False Then
Set mWB = ActiveWorkbook.Worksheets(I).Name Like "REDS*"
Debug.Print ActiveWorkbook.Worksheets(I).Name
ReDim arySheets(mWB.Worksheets.Count)
For Each oWS In mWB.Worksheets


'''my code here.




Next mWS




End With




End Sub

Artik
07-26-2019, 02:27 AM
Oh, a lot of mistakes in this code.
Try this
Sub LoopSheets()

Dim oWS As Worksheet
Dim I As Long

With ActiveWorkbook

For I = 1 To .Worksheets.Count

If UCase(.Worksheets(I).Name) Like UCase("Reds*") Then

Set oWS = .Worksheets(I)
Debug.Print oWS.Name

'''my code here.

End If

Next I

End With

End Sub

or that
Sub LoopSheets1()
Dim oWS As Worksheet

With ActiveWorkbook

For Each oWS In .Worksheets
If UCase(oWS.Name) Like UCase("RedS*") Then

Debug.Print oWS.Name

'''my code here.

End If

Next oWS

End With

End Sub
Artik