簡體   English   中英

創建數據透視表的Excel VBA

[英]Excel VBA that Creates a Pivot Table

我有這個宏,它吸收了所有sheet1並為其創建了數據透視表。 但是,當前只查看我制作時的行數,而不是整個工作表,無論那天有多少行。 有什么辦法可以使它每次都選擇全部sheet1作為數據透視表數據嗎?

如果可能的話,我還要根據列名(IDNUMBER)將修訂重復項更改為整個列。

范圍(“ $ A $ 1:$ AM $ 2428”)范圍(“ $ A $ 1:$ AM $ 4000”)“ PIVOT_STATE_REPORT!R1C1:R1048576C39”

Sub PIVOT_STATE()
'
' PIVOT_STATE Macro
'

'
    'ActiveSheet.Range("$A$1:$AM$2428").RemoveDuplicates Columns:=36, Header:= _
        'xlYes
    Columns("AJ:AJ").Select
    ActiveSheet.Range("$A$1:$AM$4000").RemoveDuplicates Columns:=36, Header:= _
        xlYes
    Range("A2").Select
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "PIVOT_STATE_REPORT!R1C1:R1048576C39", Version:=xlPivotTableVersion15). _
        CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="PivotTable1" _
        , DefaultVersion:=xlPivotTableVersion15
    Sheets("Sheet1").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("State (Corrected)")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("Count"), "Sum of Count", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("Claim Age in CS"), _
        "Sum of Claim Age in CS", xlSum
    ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
        "PivotTable1").PivotFields("Days Since LHN"), _
        "Sum of Days Since LHN", xlSum
    Range("B3").Select
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Count")
        .Caption = "Count of Count"
        .Function = xlCount
    End With
    Range("C3").Select
    With ActiveSheet.PivotTables("PivotTable1").PivotFields( _
        "Sum of Claim Age in CS")
        .Caption = "Average of Claim Age in CS"
        .Function = xlAverage
        .NumberFormat = "0"
    End With
    Range("D3").Select
    With ActiveSheet.PivotTables("PivotTable1").PivotFields( _
        "Sum of Days Since LHN")
        .Caption = "Average of Days Since LHN"
        .Function = xlAverage
        .NumberFormat = "0"
    End With
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("State (Corrected)")

    End With
End Sub

提前致謝!

我將使用此函數提供的最后一行和最后一列的公式。 然后使源成為這些變量的組合。

Function LastRowColumn(sht As Worksheet, RowColumn As String) As Long
'PURPOSE: Function To Return the Last Row Or Column Number In the Active 
Spreadsheet
'INPUT: "R" or "C" to determine which direction to search

Dim rc As Long

Select Case LCase(Left(RowColumn, 1)) 'If they put in 'row' or column instead of 'r' or 'c'.
  Case "c"
LastRowColumn = sht.Cells.Find("*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
 Case "r"
   LastRowColumn = sht.Cells.Find("*", LookIn:=xlFormulas, SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious).Row
 Case Else
  LastRowColumn = 1
End Select

 End Function

然后在您的宏內調用:

 x = LastRowColumn(ActiveSheet, "column")
 y = LastRowColumn(ActiveSheet, "Row")
 plast = cells(x,y)

然后根據數據透視表的來源確定范圍

    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"PIVOT_STATE_REPORT!A1:" & plast, Version:=xlPivotTableVersion15). _
    CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="PivotTable1" _
    , DefaultVersion:=xlPivotTableVersion15

我認為最簡單的方法是替換代碼的這些行

Columns("AJ:AJ").Select
ActiveSheet.Range("$A$1:$AM$4000").RemoveDuplicates Columns:=36, Header:=xlYes
Range("A2").Select

Dim rData As Range: Set rData = ActiveSheet.Range("A1", ActiveSheet.Range("A1").End(xlDown))
Set rData = Range(rData, rData.End(xlToRight))
rData.RemoveDuplicates Columns:=36, Header:=xlYes
ActiveWorkbook.PivotCaches.Create SourceType:=xlDatabase, SourceData:=rData

此更新是您制作PivotCache的地方

`
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
            "PIVOT_STATE_REPORT!R1C1:R1048576C39", Version:=xlPivotTableVersion15). _
            CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="PivotTable1" _
            , DefaultVersion:=xlPivotTableVersion15c
`

以下代碼使用rData范圍作為源數據

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
    rData, Version:=xlPivotTableVersion15). _
    CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="PivotTable1" _
    , DefaultVersion:=xlPivotTableVersion15

請注意,這假設您的數據的列或行中沒有空格。 End(xlDown)等效於按住Ctrl和Down鍵

暫無
暫無

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

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