我想從不同的工作表中做一個PIVOT表,並將每個PIVOT輸出插入到某個表中。如何動態定義PIVOT表源碼?
Sub PivotvpcodeSATURATION()
Dim wsNew As Worksheet
Dim i As Integer
Dim z As Integer
Set ws1 = ActiveWorkbook.Sheets("24.11.")
Set ws2 = ActiveWorkbook.Sheets("25.11.")
Set ws3 = ActiveWorkbook.Sheets("26.11.")
Set ws4 = ActiveWorkbook.Sheets("27.11.")
Set ws5 = ActiveWorkbook.Sheets("28.11.")
Set ws6 = ActiveWorkbook.Sheets("29.11.")
Set ws7 = ActiveWorkbook.Sheets("30.11.")
Set wsNew = Sheets.Add
i = 3
z = 16
Do
ws1.Select
Range("A2").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"24.11.!R1C2:R11754C10", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:=wsNew.Name & "!R3C1", TableName:="PivotTable", DefaultVersion _
:=xlPivotTableVersion14
wsNew.Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable").PivotFields("VP CODE")
.Orientation = xlRowField
.Position = 1
End With
wsNew.Select
ActiveSheet.PivotTables("PivotTable").AddDataField ActiveSheet.PivotTables(_
"PivotTable").PivotFields("1/0"), "Sum of 1/0", xlSum
wsNew.Select
Range("B4").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("VP_Saturation").Select
' Range(Cells(3, z).Address).Select
ActiveSheet.Paste
Application.DisplayAlerts = False
wsNew.Delete
Application.DisplayAlerts = True
z = z + 2
Loop Until z = 28
End Sub
我是不能夠達到的事情,
如何定義一個工作表名作爲變量,這樣我就不必選擇W1,W2,W ..,WN每次,並且不必在24.11內放置表名。!R1C2:R11754C10。
以及插入複製數據透視結果的列名稱我定義爲變量單元格(3,z).Address.Select,但此字符串返回錯誤。
也許有人可以幫忙嗎?謝謝!
謝謝,它有幫助。唯一不能管理順序循環的東西?如果我說:'對於每週DayWeek2在第二週做... z = z + 2下一個DayWeek2循環直到z = 29'它給出一個錯誤 – Ale 2014-12-10 11:18:58
你的'next'和'循環直到'是錯誤的方式,循環必須是在彼此之內,也就是說'Do'在'for'循環中,直到''next'之前'也必須在裏面 – Raugmor 2014-12-10 15:43:07