マクロを記録した後、この 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