VBA to Run Macro on Every Sheet in Workbook

ktsmith21

New Member
Joined
Feb 25, 2015
Messages
16
I'm trying to develop the code to run a macro on every sheet of the workbook, except for a few named sheets. The macro is simply copying the information from each worksheet into a list on the "Summary" sheet. I am very new to VBA (as you can see from my macro below). Here is what I have so far that is not working:

Sub SUMMARY_COPY_ALL_SHEETS()
'
' SUMMARY_COPY_ALL_SHEETS Macro
'
'
Dim ws As Worksheet
For Each ws In Worksheets
' Select case is case sensitive
Select Case UCase(ws.CodeName)
Case "Project Limits"
Case "HCSS Import"
Case "SUM_STAT"
Case "SUMMARY"
'The sheets above are the sheets that I do not want to include'
Case Else
With ws
Range("B1", ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy
Sheets("SUMMARY").Select
Range("G4").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
End With
End Select
Next ws

End Sub


I would also love to know if there is a better way of positioning the copied data onto the destination sheet. Currently, I am using the CTRL+SHIFT+DOWN method, then CTRL+SHIFT+UP, then offset one cell down to get to the right place to paste the data from the next sheet.

Thanks for any help you can give me,

Kevin
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Not knowing exactly what you are doing, I assumed copy G4 down and paste on Summary column one. Might have to tweak a little (like the range for the column which you mentioned in your OP)

Code:
Sub SummaryOfSheets()
    Dim ws As Worksheet
     
    Application.ScreenUpdating = False
    Sheets("Summary").Activate
     
    For Each ws In Worksheets
        If ws.Name <> "Summary" And ws.Name <> "Project Limits" And ws.Name <> "HCSS Import" And ws.Name <> "SUM_STAT""SUMMARY" Then
             ws.Range("G6:G500000").Copy
            Worksheets("Summary").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValues)
        End If
    Next ws
End Sub

Also you
Code:
 and
when you post codes and put the code between the tags :) (on the forums I mean)
 
Upvote 0
Thank you for your help with this. For the worksheets included in this, I'm trying to do all worksheets, except those that are named below: Summary, Project Limits, HCSS Import, SUM_STAT

I need it to run the code for all of the other worksheets. The name of the sheets will change regularly in the future.

Essentially, I want to copy the data in columns B through I, from all sheets except those named, into the Summary Sheet, beginning in cell G5. Once I paste the information from the first sheet, I need the information from the next sheet to paste, starting in the same column, underneath the last set of info. Basically building one big list of all info from the other sheets.



Not knowing exactly what you are doing, I assumed copy G4 down and paste on Summary column one. Might have to tweak a little (like the range for the column which you mentioned in your OP)

Code:
Sub SummaryOfSheets()
    Dim ws As Worksheet
     
    Application.ScreenUpdating = False
    Sheets("Summary").Activate
     
    For Each ws In Worksheets
        If ws.Name <> "Summary" And ws.Name <> "Project Limits" And ws.Name <> "HCSS Import" And ws.Name <> "SUM_STAT""SUMMARY" Then
             ws.Range("G6:G500000").Copy
            Worksheets("Summary").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValues)
        End If
    Next ws
End Sub

Also you
Code:
 and
when you post codes and put the code between the tags :) (on the forums I mean)
 
Upvote 0
Ok I got it working right - I did not realize the sheet names were case sensitive. I adjusted those and moved the location on Summary sheet to paste to. Works perfectly. Thank you very much for your help with this!
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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