簡體   English   中英

Excel VBA:不管數據透視表中的行數如何,都將行移動到目標

[英]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.

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