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

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Ok. Just ran into an issue. I ran this on some test data and it worked fine but when I applied it to my actual data it didn't work correctly because the data in my sheets are formulas. How can I change the copy paste code to paste values?
 
Upvote 0
try something like this...

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).PasteSpecial Paste:=xlPasteValues
   End If
Next WS
End Sub
 
Upvote 0
Hmm....I got a compile error.

1677599811240.png
 
Upvote 0
Like this? I've got something wonky in my syntax.

WS.Range("CA5:CO589").Copy sheets("GL Detail").Range("A" & lrow + 1).PasteSpecial Paste:=xlPasteValues
Sheets ("GL Detail")
 
Upvote 0
No...exactly how I put it in post #13

Separate the copy and paste portions to different lines
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,743
Members
449,094
Latest member
dsharae57

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