Динамический диапазон VBA Pivot Table - PullRequest
0 голосов
/ 11 апреля 2019

Я создал макрос для создания сводной таблицы из источника данных. Поэтому я хочу использовать его для другого источника даты, но диапазон может измениться. Любой знает, как добавить динамический диапазон, чтобы использовать этот макрос с любым диапазоном данных.

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



На самом деле код работает отлично, но, как я уже говорил, диапазон не является динамическим.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...