简体   繁体   中英

Pivot Table - VBA

I'd like to create a pivot table which can be automatically updated by choosing the sheet where it will take the information. I have 40 sheets iso-formatted and I tried this :

Sub tcd()
Dim CR As String
    CR = InputBox("Num of CR")
    Sheets(CR).Select
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        CR & "!" & Sheets(CR).Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1), _
        Version:=xlPivotTableVersion14).CreatePivotTable _
        TableDestination:="Feuil1!R2C2", TableName:="Sum", _
        DefaultVersion:=xlPivotTableVersion14
     Sheets("Feuil1").Select
     Cells(2, 2).Select
     Range("C8").Select
    With ActiveSheet.PivotTables("Sum").PivotFields( _
        "N1")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("Sum").AddDataField ActiveSheet. _
        PivotTables("Sum").PivotFields("N2"), _
        "Num of N2", xlCount
    Sheets("SMM").Select
End Sub

Unfortunately it doesn't seem to work and I can't find out why and thus, how to solve this.

EDIT1 : It gives me the error '5' : Invalid procedure call or argument.

EDIT 2 : Concerning the data

例

The data contains 6 columns actually but only the first 3 are important. I can't predict the number of row because it depends on the sheet that will be used.

If you have any idea ? I thank you in advance for your answers !

Reading your post, I assume you enter the Worksheet name in the InputBox , therefore CR is a String consists of the Worksheet name that holds your Pivot Table's data.

(I removed all the Select from the code, they are not needed to create the PivotTable you want).

Sub tcd()

Dim CR                  As String
Dim PTCache             As PivotCache
Dim PT                  As PivotTable
Dim wsSheet             As Worksheet
Dim PivotShtExists      As Boolean


CR = InputBox("Num of CR") ' assuming you enter the name of your worksheet's data in the input box

' check is "Feuil1" sheet already exists (from previous Macro runs)
For Each wsSheet In ThisWorkbook.Sheets
    If wsSheet.Name = "Feuil1" Then
        PivotShtExists = True
    End If
Next wsSheet

If Not PivotShtExists Then Sheets.Add.Name = "Feuil1"    

' add a new Pivot Cache
Set PTCache = ActiveWorkbook.PivotCaches.Add(xlDatabase, CR & "!" & Sheets(CR).Range("A1").CurrentRegion.Address(ReferenceStyle:=xlR1C1))

' add this line in case the Pivot table doesn't exit >> first time running this Macro
On Error Resume Next
Set PT = Sheets("Feuil1").PivotTables("Sum") ' check if "Sum" Pivot Table already created (in past runs of this Macro)

On Error GoTo 0
If PT Is Nothing Then

    ' create a new Pivot Table in "Feuil1" sheet
    Set PT = Sheets("Feuil1").PivotTables.Add(PivotCache:=PTCache, TableDestination:="Feuil1!R2C2", TableName:="Sum")

    With PT.PivotFields("N1")
        .Orientation = xlRowField
        .Position = 1
    End With

    PT.AddDataField PT.PivotFields("N2"), "Num of N2", xlCount

Else
    ' just refresh the Pivot cache with the updated Range
    PT.ChangePivotCache PTCache
    PT.RefreshTable
End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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