To create a pivot table from data set with VBA

TopLearner

Board Regular
Joined
Feb 25, 2022
Messages
57
Office Version
  1. 365
Platform
  1. Windows
Hello there,



I hope you are well.



I have data on sheet “Sheet1”

I would like to create a pivot table on a separate tab called "Pivot" with this data using VBA. I have already tried but without success.

"Sheet1" tab

1662998079783.png



"Pivot" tab

1662998101512.png

I would like to present the table in tabular form. I get stuck with the code.


VBA Code:
Dim ws As Worksheet

Dim newws As Worksheet

Dim PTcache As PivotCache

Dim PT As PivotTable

Dim Pivrang As String

Dim Startpiv As String





Set ws = ActiveSheet

Pivrang = ws.Activate. Address(ReferenceStyle:=xlR1C1)

Cells.Select



Set news = Sheets.Add



Startpiv = ws.Range (“A3”). Address(ReferenceStyle:=xlR1C1)



Set pvtCache = ActiveWorkbook.PivotCaches.Create( _

SourceType:=xlDatabase, _

SourceData:=SrcData)



Set pvt = pvtCache.CreatePivotTable( _

TableDestination:=StartPvt, _

TableName:="PivotTable1")



I am hoping you can help me by providing the code.

Thanks
Sanchez
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi, TopLearner, where does your data start on Sheet1?

The following code below assumes that your data is starting on Range("A1") like it does on my sheet(see the link below for an example as to what my data looked like).

VBA Code:
Sub TopLearner()
    Dim pvtCache As PivotCache
    Dim pvt As PivotTable


    'Creating PivotCaches
    Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
                      SourceType:=xlDatabase, _
                      SourceData:=Sheets("sheet1").Range("A1").CurrentRegion)

 

    'Creating PivotTables (from PivotCaches)
    Set pvt = pvtCache.CreatePivotTable(TableDestination:=Sheets("Pivot").Range("A3"))
 

    'Setting fields
    With pvt
        .PivotFields("Employee First Name").Orientation = xlRowField
        .PivotFields("Employee Last Name").Orientation = xlRowField
        .PivotFields("Department Name").Orientation = xlRowField
        .PivotFields("Pay Code Description").Orientation = xlRowField
        .PivotFields("Hours").Orientation = xlDataField    'data field
    End With

End Sub

Have a look at this thread. I posted a similar question in recently. Hope this helps!

 
Upvote 0
Hi, TopLearner, where does your data start on Sheet1?

The following code below assumes that your data is starting on Range("A1") like it does on my sheet(see the link below for an example as to what my data looked like).

VBA Code:
Sub TopLearner()
    Dim pvtCache As PivotCache
    Dim pvt As PivotTable


    'Creating PivotCaches
    Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
                      SourceType:=xlDatabase, _
                      SourceData:=Sheets("sheet1").Range("A1").CurrentRegion)

 

    'Creating PivotTables (from PivotCaches)
    Set pvt = pvtCache.CreatePivotTable(TableDestination:=Sheets("Pivot").Range("A3"))
 

    'Setting fields
    With pvt
        .PivotFields("Employee First Name").Orientation = xlRowField
        .PivotFields("Employee Last Name").Orientation = xlRowField
        .PivotFields("Department Name").Orientation = xlRowField
        .PivotFields("Pay Code Description").Orientation = xlRowField
        .PivotFields("Hours").Orientation = xlDataField    'data field
    End With

End Sub

Have a look at this thread. I posted a similar question in recently. Hope this helps!

Thank you Coyotex3

My data starts in A1 on Sheet1 and takes several cells whose number varies depending of the month the data belongs to

SourceData:=Sheets("sheet1").Range("A1").CurrentRegion)

By writing the above line, does it mean I am taking all the cells that are not blank on Sheet1?

Thanks again
Sanchez
 
Upvote 0
Hi, TopLearner, where does your data start on Sheet1?

The following code below assumes that your data is starting on Range("A1") like it does on my sheet(see the link below for an example as to what my data looked like).

VBA Code:
Sub TopLearner()
    Dim pvtCache As PivotCache
    Dim pvt As PivotTable


    'Creating PivotCaches
    Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
                      SourceType:=xlDatabase, _
                      SourceData:=Sheets("sheet1").Range("A1").CurrentRegion)

 

    'Creating PivotTables (from PivotCaches)
    Set pvt = pvtCache.CreatePivotTable(TableDestination:=Sheets("Pivot").Range("A3"))
 

    'Setting fields
    With pvt
        .PivotFields("Employee First Name").Orientation = xlRowField
        .PivotFields("Employee Last Name").Orientation = xlRowField
        .PivotFields("Department Name").Orientation = xlRowField
        .PivotFields("Pay Code Description").Orientation = xlRowField
        .PivotFields("Hours").Orientation = xlDataField    'data field
    End With

End Sub

Have a look at this thread. I posted a similar question in recently. Hope this helps!

Hi again,

Would it be possible to add to the macro the creation of the sheet "Pivot" as it is not supposed to be manually created?
Also, how I could do so that the different fields "Employee First Name", "Employee Last Name", "Department Name", "Pay Code Description" are presented in a tabular layout?

Thanks
Sanchez
 
Upvote 0
Thank you Coyotex3

My data starts in A1 on Sheet1 and takes several cells whose number varies depending of the month the data belongs to

SourceData:=Sheets("sheet1").Range("A1").CurrentRegion)

By writing the above line, does it mean I am taking all the cells that are not blank on Sheet1?

Thanks again
Sanchez
Current Region is like pressing CTRL + * to select a range.
 
Upvote 0
Hi TopLearner, give this one a try to see if it does what you need.

VBA Code:
Sub TopLearner1()
    Dim pvtCache As PivotCache
    Dim pvt As PivotTable

    Sheets.Add.Name = "Pivot"          ' Create a new worksheet called "Pivot"
    'Creating PivotCaches
    Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
                      SourceType:=xlDatabase, _
                      SourceData:=Sheets("sheet1").Range("A3").CurrentRegion)

   

    'Creating PivotTables (from PivotCaches)
    Set pvt = pvtCache.CreatePivotTable(TableDestination:=Sheets("Pivot").Range("A3"))
    

    'Setting fields
    With pvt
        .PivotFields("Employee First Name").Orientation = xlRowField    'row field
        .PivotFields("Employee Last Name").Orientation = xlRowField
        .PivotFields("Department Name").Orientation = xlRowField
        .PivotFields("Pay Code Description").Orientation = xlRowField
        .PivotFields("Hours").Orientation = xlDataField    'data field
    End With

    With Sheets("Pivot")
        pvt(1).RowAxisLayout xlTabularRow
    End With

End Sub
 
Upvote 0
Hi TopLearner, give this one a try to see if it does what you need.

VBA Code:
Sub TopLearner1()
    Dim pvtCache As PivotCache
    Dim pvt As PivotTable

    Sheets.Add.Name = "Pivot"          ' Create a new worksheet called "Pivot"
    'Creating PivotCaches
    Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
                      SourceType:=xlDatabase, _
                      SourceData:=Sheets("sheet1").Range("A3").CurrentRegion)

  

    'Creating PivotTables (from PivotCaches)
    Set pvt = pvtCache.CreatePivotTable(TableDestination:=Sheets("Pivot").Range("A3"))
   

    'Setting fields
    With pvt
        .PivotFields("Employee First Name").Orientation = xlRowField    'row field
        .PivotFields("Employee Last Name").Orientation = xlRowField
        .PivotFields("Department Name").Orientation = xlRowField
        .PivotFields("Pay Code Description").Orientation = xlRowField
        .PivotFields("Hours").Orientation = xlDataField    'data field
    End With

    With Sheets("Pivot")
        pvt(1).RowAxisLayout xlTabularRow
    End With

End Sub
Many thanks. Only two questions.
If I want the pay code description to be in the filters field, would I have to type in xlFilterField?

Also, by default subtotals is automatic, how could I set none?

1666004778516.png



Thanks
Sanchez
 
Upvote 0

Forum statistics

Threads
1,214,573
Messages
6,120,318
Members
448,956
Latest member
Adamsxl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top