[英]Excel VBA: Move row to a destination regardless of row count in pivot table
我是VBA的新手,並且具有非常基礎的知識。 我有一個數據透視表,該數據表可按入學日期對學生計數,列由Brand Ids
組成,我想將一行移到數據透視表的末尾,並且行數不斷變化。 我嘗試下面的代碼,但只有在數據透視表中的行數恆定時才能使用它。 盡管行數不是恆定的,有人可以幫我將行移到表的末尾嗎?
ActiveSheet.PivotTables("StudentCount").PivotSelect "'En_Date'['(blank)']", _
xlDataAndLabel + xlFirstRow, True
ActiveSheet.PivotTables("StudentCount").PivotFields("En_Date").PivtoItems( _
"(blank)").Position = 20
'In current sheet I need to move to 20 but actually 20 is not constant
ActiveSheet.PivotTables("StudentCOunt").PivotSelect "En_Date", xlButton, True
我也嘗試了下面的代碼,但它不起作用:
Dim LS as LONG
LS = ActiveSheet.PivotTable("StudentCount").TableRange2.Rows.Count
ActiveSheet.PivotTables("StudentCount").PivotSelect "'En_Date'['(blank)']", _
xlDataAndLabel + xlFirstRow, True
ActiveSheet.PivotTables("StudentCount").PivotFields("En_Date").PivtoItems( _
"(blank)").Position = "& (LS - 1)"
ActiveSheet.PivotTables("StudentCOunt").PivotSelect "En_Date", xlButton, True
數據透視表:
Count Of Students Date A1 A2 A3 A4 Blank Grand TOtal blank 69 86 23 45 223 01/01/2014 2 25 3 1 31 02/01/2014 1 2 5 8 16 Grand Total 71 112 28 51 262
您好,回答您的問題,您不需要VBA
即可執行此操作。
您的數據透視表這樣做的原因是因為您的Dates
實際上不是Dates
而是Dates Entered as Text
。
請參閱以下重新創建的數據:
重新輸入它作為實際dates
將解決您的問題。
因此,在將“ Text Dates
更改為實際Dates
,樞軸將根據需要對行進行排序。
見下文:
希望我提出的解決方案對您有所幫助。
另外:如果重新輸入所有日期不方便,請嘗試以下代碼:
Dim i As Integer
With ActiveSheet.PivotTables("StudentCount").PivotFields("En Date")
For i = 1 To .PivotItems.Count
If .PivotItems(i) = "(blank)" Then .PivotItems(i).Position = .PivotItems.Count
Next
End With
上面的代碼只是簡單地遍歷所有PivotItems
,查找(blanks)
。
然后,將其位置更改為等於.PivotItems.Count
的最后一個位置。
希望這對您有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.