Loop through Worksheet with certain names

pikeman56

New Member
Joined
Aug 10, 2006
Messages
38
Hello all - I need help with a macro. I have a workbook with multiple worksheets. I need to create a macro to copy data from certain worksheets Ex: Range(A4:F20) only from worksheets with a name that starts with the number 5. Example the names can be 5432, 5542, 5998 etc. I'm having trouble getting my loop to work.

Thanks.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Dim WS As Worksheet

For Each WS In ActiveWorkbook.Worksheets
If WS.Name Like "5" & "*" Then
Range("CA5:CO589").Select

End If
Next WS
 
Upvote 0
I think you’re close. Try something like this:
VBA Code:
Private sub copydata()
Dim WS As Worksheet

For Each WS In Worksheets
If WS.Name = "5*” Then
WS.Range("CA5:CO589").copy destination:=sheet12.range(“A1”)

End If
Next WS
End sub

I did this on my phone so I haven’t tested it out…but should be close. Also…I have no idea where you want to paste to so you’ll need to update your destination.
 
Upvote 0
I tested the above loop just to see if I can select the correct ranges and get the following error.

1677538462677.png
 
Upvote 0
The problem there is there is a weird " after the 5*. Replace the quotes and it should run fine.
 
Upvote 0
Here's what I ended up with that worked:

VBA Code:
Private Sub copydata()
Dim WS As Worksheet

For Each WS In ActiveWorkbook.Worksheets
   If WS.Name Like "5*" Then
        WS.Range("A5:Z20").Copy Sheets("GL Detail").Range("A1")
   End If
Next WS
End Sub
 
Upvote 0
Taking that one step further...I assume you'll want to paste the range to the next available row on GL Detail. Give something like this a try.

VBA Code:
Private Sub copydata()
Dim WS As Worksheet
Dim lrow As Long

For Each WS In ActiveWorkbook.Worksheets
   If WS.Name Like "5*" Then
   lrow = Sheets("GL Detail").Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
        WS.Range("A5:Z20").Copy Sheets("GL Detail").Range("A" & lrow + 1)
   End If
Next WS
End Sub
 
Upvote 0
Awesome. This works perfectly. I had to change a few of the ranges etc but it's good to go. Thanks a ton!!!
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,942
Members
449,094
Latest member
teemeren

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