Runtime error 1004 while compile vba with PivotTable

Stephen Yip

New Member
Joined
Jun 12, 2015
Messages
2
I am now encounter a "simple" problem but no clue on it.

I get the following code for creating Pivot table from a website(stated in the comment part)

Sub CreatePivotTable()
'PURPOSE: Creates a brand new Pivot table on a new worksheet from data in the ActiveSheet
'Source: Squarespace - Claim This Domain

Dim sht As Worksheet
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim StartPvt As String
Dim SrcData As String

Range("A5").Select

'Determine the data range you want to pivot
SrcData = ActiveSheet.Name & "!" & ActiveSheet.Range("A5:R100").Address(ReferenceStyle:=xlR1C1)

'Create a new worksheet
Set sht = Sheets.Add

'Where do you want Pivot Table to start?
StartPvt = sht.Name & "!" & sht.Range("A3").Address(ReferenceStyle:=xlR1C1)

'Create Pivot Cache from Source Data
Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=SrcData)

'Create Pivot table from Pivot Cache
Set pvt = pvtCache.CreatePivotTable( _
TableDestination:=StartPvt, _
TableName:="PivotTable1")


End Sub





It runs everything fine however, I want to change the source data into a more dynamic way and make the following changes

Range(Range("A5"),Range("A5").SpecialCells(xlLastCell)) instaed of Range("A5:R100")

Runtime error 1004: Application-defined or object-defined error comes out at the code yellow in color

I googled the error but seems cant solve my problem.

What will be the possibility?

Looking for your advices.

Thank you
 
Last edited:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,203,600
Messages
6,056,206
Members
444,850
Latest member
dancasta7

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