簡體   English   中英

不論工作表或工作簿名稱如何,都將一個宏應用於另一工作表/工作簿

[英]Apply one macro to another sheet/workbook regardless of sheet or workbook name

我有一個宏返回錯誤的問題,我想知道你們是否可以闡明這個問題。

我擁有的宏如下所示:

Sub pivot1_macro()
'
' pivot1_macro Macro
'
' Keyboard Shortcut: Ctrl+Shift+M
'
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    "7-14 week!R1C2:R199C35", Version:=xlPivotTableVersion15).CreatePivotTable _
    TableDestination:="Sheet5!R3C1", TableName:="PivotTable3", DefaultVersion _
    :=xlPivotTableVersion15
Sheets("Sheet5").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable3").PivotFields("PartMOD")
    .Orientation = xlColumnField
    .Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("EnteredShift")
    .Orientation = xlRowField
    .Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Defect")
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
    "PivotTable3").PivotFields("Defect Quantity"), "Sum of Defect Quantity", xlSum
Columns("D:D").EntireColumn.AutoFit
Columns("A:A").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("C:C").EntireColumn.AutoFit
Columns("D:D").EntireColumn.AutoFit
Range("B3").Select
Range("B3").Select
End Sub

我想將此宏應用於任何活動的工作表,但我不確定如何將'SourceData'設置為當前的活動工作表。

您可以使用“&”連接字符串,因此不會

SourceData := ActiveSheet.Name & "!R1C2:R199C35"

為您工作以獲取當前工作表的名稱?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM