VBA 程式碼說明 - 將多張工作表複製到新工作簿中(用於分發)

VBA 程式碼說明 - 將多張工作表複製到新工作簿中(用於分發)

我在錄製巨集後編輯此 vba 程式碼時遇到問題。宏代碼如下。我收到一條錯誤,指出「wsSplitBU.Copy After:=Aname.Sheets(1)」行中的限定符無效。

這裡的想法是將這 4 張工作表複製到一個全新的工作簿中,然後在儲存之前將它們貼為值。

這裡的專家是否能夠幫助編輯程式碼使其更加靈活?

謝謝你!

   Sub Data_Cleanser()


Application.ScreenUpdating = False

Dim wsRaw As Worksheet
Set wsRaw = Sheets("RAW DATA")
Dim wsPivot As Worksheet
Set wsPivot = Sheets("Pivot_RAW_DATA")
Dim wsPivotM As Worksheet
Set wsPivotM = Sheets("Pivot")
Dim lastRowRD As Long
lastRowRD = wsRaw.Cells(Rows.Count, "A").End(xlUp).Row
Dim wbS As Workbook
Set wbS = Workbooks("Spend automator.xlsm")
Dim wsSplitBU As Worksheet
Set wsSplitBU = Sheets("Split BU (HUTAS)")
Dim wsLocalS As Worksheet
Set wsLocalS = Sheets("Localization Spend")
Dim wsPlantSp As Worksheet
Set wsPlantSp = Sheets("Bedok, Changi, Bandung Spend")

''''''''''''''''''''''''''''''
'Populate formula'
''''''''''''''''''''''''''''''

[Aa1].Resize(lastRowRD - 1, 1).FormulaR1C1 = ("BU Correction Generator")
[Aa2].Resize(lastRowRD - 1, 1).Formula = ("=VLOOKUP(N2,'BU CORRECTOR REFERENCE'!$A:$C,3,FALSE)")

''''''''''''''''''''''''''''''
'Refresh Pivot'
''''''''''''''''''''''''''''''

wsPivot.Select
ActiveSheet.PivotTables("PivotTable9").PivotCache.Refresh
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
ActiveSheet.PivotTables("PivotTable2").PivotCache.Refresh
wsPivotM.Select
ActiveSheet.PivotTables("PivotTable3").PivotCache.Refresh

Dim Aname As String
Aname = ActiveWorkbook.Sheets(1).Range("A1").Value
Workbooks.Add

''''''''''''''''''''''''''''''
'Create new distributable workbook'
''''''''''''''''''''''''''''''

    wsPivotM.Copy
    wbS.Activate
    wsSplitBU.Copy After:=Aname.Sheets(1)
    wbS.Activate
    wsLocalS.Copy After:=Aname.Sheets(2)
    wbS.Activate
    wsPlantSp.Copy After:=Aname.Sheets( _
    3)
    Range("B4:M8").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Localization Spend").Select
    Range("B3:M19").Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("L1:M1").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("L2").Select
    ActiveSheet.Paste
    Sheets("Split BU (HUTAS)").Select
    ActiveWindow.ScrollColumn = 9
    ActiveWindow.ScrollColumn = 3
    Range("C18:N46").Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("M1:N1").Select
    Application.CutCopyMode = False
    Selection.Copy
    Range("M2").Select
    ActiveSheet.Paste
    Sheets("Pivot").Select
    Application.CutCopyMode = False
    ActiveWorkbook.SaveAs Filename:=SpendReport & ".xls"


End Sub

答案1

@akina 你能舉個例子嗎? – Fadhilah Afif 19 分鐘前

將此工作簿中的工作表複製到另一個工作簿範例中。

Sub copy_sheet()
    Workbooks.Open Filename:="c:\tmp\workbook2.xlsx"
    ThisWorkbook.Sheets("Sheet1").Copy Before:=Workbooks("workbook2.xlsx").Sheets("Sheet2")
    Workbooks("workbook2.xlsx").Save
    Workbooks("workbook2.xlsx").Close
End Sub

相關內容