简体   繁体   中英

Copy and Paste Macro for worksheets that will change name

I'm trying to write a simple macro to copy/paste data from multiple tabs into a summary tab. My issue is that for every project the sample tabs will change names. Once I change the name of the sample tab my macro no longer works.

Is there a way to link to Sheet 1, Sheet 2, Sheet 3... etc without changing the macro every time? Here is some of my code, and I've the parts called Sample, or Sample (2) are what needs to change every project.

Thanks in advance!

Sheets("Sample").Select
    Range("B15:B29").Select
    Selection.Copy
    Sheets("Data Summary").Select
    Range("B15").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Sample (2)").Select
    Range("B15:B29").Select
    Application.CutCopyMode = False
    Selection.Copy
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("Data Summary").Select
    Range("C15").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

This isn't the best solution, but it should work. Before you run the macro you will need to make sure that your summary sheet is the active worksheet.

Dim Summary As Worksheet: Set Summary = ActiveSheet
Sheets("Sample").Select
Range("B15:B29").Select
Selection.Copy
Summary.Select
Range("B15").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Sheets("Sample (2)").Select
Range("B15:B29").Select
Application.CutCopyMode = False
Selection.Copy
Application.CutCopyMode = False
Selection.Copy
Summary.Select
Range("C15").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

What this does is store a reference to the active sheet (your summary) and uses that reference to recall the sheet when copying the data.

Creating references to worksheets ( Dim Sheet As Worksheet , and then assigning a sheet to it) allows you to access the data on that sheet without having to activate it first which can help to simplify your code. The example below should have the same result as the one above. The summary sheet still needs to be active before you run it.

Dim Summary As Worksheet: Set Summary = ActiveSheet
Dim S1 As Worksheet: Set S1 = ThisWorkbook.Worksheets("Sample")
Dim S2 As Worksheet: Set S2 = ThisWorkbook.Worksheets("Sample (2)")

Application.ScreenUpdating = False

S1.Range("B15:B29").Copy
Summary.Range("B15").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False


S2.Range("B15:B29").Copy
Summary.Range("C15").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

Application.ScreenUpdating = True

More examples! this one allows you to pre-define how many "sample" sheets you have without having to code each one individually (hopefully, haven't tested it)

Dim Summary As Worksheet: Set Summary = ActiveSheet
Dim Source As Worksheet
Dim Index As Integer
Dim Name As String

Application.ScreenUpdating = False

' index represents the range of your sample sheets
' below my example assumes you have 5
For Index = 1 To 5
    ' generate the name of the worksheet
    Name = "Sample" & IIf(Index > 1, " (" & Index & ")", "")
    Set Source = ThisWorkbook.Worksheets(Name)
    Source.Range("B15:B29").Copy
    ' using .Cells instead of .Range allows you to reference cells numerically
    ' so below its row 15 and the column changes depending on the index
    ' the +1 is there as your example starts at column B
    Summary.Cells(15, Index + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Next Index

Application.ScreenUpdating = True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM