动态范围VBA数据透视表

时间:2019-04-11 02:21:16

标签: excel vba

我已经构建了一个宏,可以根据数据源创建数据透视表。因此,我想将其用于其他日期来源,但范围可能会改变。任何人都知道如何添加动态范围,以便将该宏用于任何种类的数据。

Sub AuditReport()
'
' Pivotable Macro
'

'
    Columns("A:A").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("B1").Select
    Selection.AutoFill Destination:=Range("A1:B1"), Type:=xlFillDefault
    Range("A1:B1").Select
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "=RC[1]&RC[12]&RC[13]"
    Range("A2").Select
    Selection.AutoFill Destination:=Range("A2:A14264")
    Range("A2:A14264").Select
    Sheets("Result").Select
    Columns("A:A").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
    Range("B1").Select
    Selection.AutoFill Destination:=Range("A1:B1"), Type:=xlFillDefault
    Range("A1:B1").Select
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "=RC[1]&RC[12]&RC[13]"
    Range("A2").Select
    Selection.AutoFill Destination:=Range("A2:A4751")
    Range("A2:A4751").Select
    ActiveWindow.SmallScroll ToRight:=12
    Range("O1,R1:T1").Select
    Range("R1").Activate
    Selection.Copy
    Range("U1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    ActiveWindow.SmallScroll ToRight:=4
    Range("X1").Select
    Selection.AutoFill Destination:=Range("X1:Y1"), Type:=xlFillDefault
    Range("X1:Y1").Select
    Range("Y1").Select
    ActiveCell.FormulaR1C1 = "Discrepancy"
    Range("U2").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,Class,15,FALSE)"
    Selection.AutoFill Destination:=Range("U2:X2"), Type:=xlFillDefault
    Range("U2:X2").Select
    Range("V2").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,Class,18,FALSE)"
    Range("W2").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,Class,19,FALSE)"
    Range("X2").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC1,Class,20,FALSE)"
    Range("U2:X2").Select
    Selection.AutoFill Destination:=Range("U2:X4751")
    Range("U2:X4751").Select
    Range("Y2").Select
    ActiveCell.FormulaR1C1 = "=IF(RC[-4]<>RC[-10],""YES"",""NO"")"
    Selection.AutoFill Destination:=Range("Y2:Y4751")
    Range("Y2:Y4751").Select
    Sheets("Pivot").Select
    Range("A1").Select
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Result!R1C1:R4751C25", Version:=xlPivotTableVersion15).CreatePivotTable _
        TableDestination:="Pivot!R1C1", TableName:="PivotTable5", DefaultVersion _
        :=xlPivotTableVersion15
    Sheets("Pivot").Select
    Cells(1, 1).Select
    ActiveWorkbook.ShowPivotTableFieldList = True
    With ActiveSheet.PivotTables("PivotTable5").PivotFields("Discrepancy")
        .Orientation = xlPageField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable5").PivotFields("Regime ")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable5").PivotFields("Classification ")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("PivotTable5").PivotFields("Item Number 2")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("PivotTable5").PivotFields("Classification ")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("PivotTable5").PivotFields("Classification 2")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable5").AddDataField ActiveSheet.PivotTables( _
        "PivotTable5").PivotFields("Item Number "), "Count of Item Number ", xlCount
    ActiveSheet.PivotTables("PivotTable5").PivotFields("Count of Item Number "). _
        Caption = "xClass Audit Report"
    ActiveWorkbook.ShowPivotTableFieldList = False
    ActiveSheet.PivotTables("PivotTable5").PivotFields("Discrepancy").CurrentPage _
        = "(All)"
    ActiveSheet.PivotTables("PivotTable5").PivotFields("Discrepancy"). _
        EnableMultiplePageItems = True
    Range("B3").Select
    ActiveSheet.PivotTables("PivotTable5").CompactLayoutColumnHeader = "Classified"
    Range("A4").Select
    ActiveSheet.PivotTables("PivotTable5").CompactLayoutRowHeader = "Superseded"
    Range("A4,B3").Select
    Range("B3").Activate
    Selection.Font.Size = 12
    Selection.Font.Size = 12
    Range("C1").Select
End Sub



实际上,代码运行良好,但是正如我提到的,范围不是动态的。

0 个答案:

没有答案