2017-06-22 300 views
0

我有这个宏需要所有sheet1并创建它的数据透视表。但是,目前它只查看我制作时的行数,而不管当天有多少行。有没有办法让每次都选择sheet1作为数据透视表数据?创建数据透视表的Excel VBA

如果可能的话,我也想修改重复项为基于列名(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 

提前感谢!

回答

0

我会使用此函数中提供的最后一行和列公式。 然后让源代码组合这些变量。

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 
0

我认为做的最简单的方法这将取代你的代码这些行

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 

更新此情况下,您使用RDATA范围为源数据

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

注意这个假设让你PivotCache

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

用下面的代码数据的列或行中没有空白。结束(xlDown)相当于按住Ctrl和Down键

+0

您好, 我收到一个错误(类型不匹配),它突出显示了查询中的下面一行。 ActiveWorkbook.PivotCaches.Create SourceType:= xlDatabase,SourceData:= rData –

+0

你可以展示你的最新代码,当我测试它时它工作正常吗? – maxhob17