简体   繁体   English

创建数据透视表的Excel VBA

[英]Excel VBA that Creates a Pivot Table

I have this macro that takes all of sheet1 and creates a pivot table of it. 我有这个宏,它吸收了所有sheet1并为其创建了数据透视表。 However, it is currently only looking at the amount of rows I had when I made it and not the entire sheet no matter how many rows it has that day. 但是,当前只查看我制作时的行数,而不是整个工作表,无论那天有多少行。 Is there any way to make it select all of sheet1 as the pivot table data every time? 有什么办法可以使它每次都选择全部sheet1作为数据透视表数据吗?

If possible I would also like to alter the fix duplicates to be the entire column based on the column name (IDNUMBER) if able. 如果可能的话,我还要根据列名(IDNUMBER)将修订重复项更改为整个列。

Range("$A$1:$AM$2428") Range("$A$1:$AM$4000") "PIVOT_STATE_REPORT!R1C1:R1048576C39" 范围(“ $ 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

Thanks in advance! 提前致谢!

I would use the last row and column formulas provided in this function. 我将使用此函数提供的最后一行和最后一列的公式。 Then make the source a combo of these variables. 然后使源成为这些变量的组合。

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

Then inside of your macro call out: 然后在您的宏内调用:

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

Then make a range based off that for the source of the pivot table 然后根据数据透视表的来源确定范围

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

I think the easiest way of doing this will be to replace these lines of your code 我认为最简单的方法是替换代码的这些行

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

With

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

The update this be where you make your PivotCache 此更新是您制作PivotCache的地方

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

With the following code using the rData range as the Source Data 以下代码使用rData范围作为源数据

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

Note this assumes that there are no gaps in the columns or rows of your Data. 请注意,这假设您的数据的列或行中没有空格。 The End(xlDown) is the equivalent of holding the Ctrl and Down key End(xlDown)等效于按住Ctrl和Down键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM