VBA - Insert pivot table into existing sheet on next available row

ravecake

New Member
Joined
Oct 26, 2022
Messages
13
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi,

I have a workbook where the first sheet ("main") is a sheet with pivot tables based on the data in each other sheet in the workbook. These other sheets are data sheets (by date) extracted from an external database. I add them to this workbook every day as follows: [ main ] [ 20230110 ] [ 20230109 ] [ 20230106 ] etc.

I need a macro that will create a pivot table based on the available data in the second sheet (which represents the current day's data), and then insert that table in the first sheet "main" on the next available empty row in Column A. Because I will be doing this repeatedly with new data I need the macro to dynamically give each pivot table a unique name to avoid duplicates.

I have the following code but keep running into 'An object is required' errors. Any help would be greatly appreciated!

VBA Code:
Sub CreateDynamicPivotTable()

    Dim mainSheet As Worksheet
    Set mainSheet = ActiveWorkbook.Sheets("main")
    Dim dataSheet As Worksheet
    Set dataSheet = ActiveWorkbook.Sheets(2)

    Dim lastRow As Long
    lastRow = mainSheet.Cells(mainSheet.Rows.Count, "A").End(xlUp).Row

    Dim ptDestination As Range
    Set ptDestination = mainSheet.Range("A" & lastRow + 1)

    Dim pivotCache As PivotCache
    Set pivotCache = ActiveWorkbook.PivotCaches.Create( _
        SourceType:=xlDatabase, _
        SourceData:=dataSheet.Range("A1").CurrentRegion)

    Dim pivotTableName As String
    pivotTableName = "PivotTable_" & Format(Now, "yyyymmdd_hhmmss")

    Dim pivotTable As PivotTable
    Set pivotTable = pivotCache.CreatePivotTable( _
        TableDestination:=ptDestination, _
        TableName:=pivotTableName)

End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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