Pivotcache sourcedata change 1004 error

magnet

New Member
Joined
Feb 9, 2012
Messages
1
I would really appreciate any help on this. Have tried various ways to update the pivotcache source data and consistently get the ]1004 Application or object-defined error' when it gets to the red code where it tries to change the sourcedata.

The various methods I have tried are remmed out. I am running this from MS Access. Thank you for any help.

Code:
[SIZE=1]Sub TestPivot()[/SIZE]
[SIZE=1]On Error GoTo Err_FormatSpreadsheets_Click[/SIZE]
[SIZE=1]Dim db As Database
Dim xl As Excel.Application
Dim xlw As Excel.Workbook
Dim xlsh As Excel.Worksheet
Dim xlshp As Excel.Worksheet
Dim filenm As String
Dim rng As Range
Dim pt As PivotTable
Dim ptc As PivotCache
Dim straddr As String[/SIZE]
[SIZE=1][/SIZE] 
[SIZE=1]Set db = CodeDb[/SIZE]
[SIZE=1]Set xl = CreateObject("Excel.Application")
Set xlw = xl.Workbooks.Open("C:\WORKINGFOLDERS\Office\AppTest\EasternPiv.xls")[/SIZE]
[SIZE=1]
xl.Visible = True[/SIZE]
[SIZE=1]Set xlsh = xlw.Worksheets("ProvList")[/SIZE]
[SIZE=1][/SIZE] 
[SIZE=1]With xlsh
    .Select
    .Cells.Select[/SIZE]
[SIZE=1]Set rng = xlsh.Range(.Cells(1, 1), xlsh.UsedRange.Cells.SpecialCells(xlLastCell))
[COLOR=seagreen][/COLOR][/SIZE]
[SIZE=1][COLOR=seagreen]'rng.Name = "PivRangeName"
'MsgBox rng.Address
'MsgBox rng.Address(1, 1, xlR1C1)
'straddr = "'" & xlsh.Name & "'!" & rng.Address(1, 1, xlR1C1)

[/COLOR][/SIZE][SIZE=1]straddr = xlsh.Name & "!" & rng.Address(1, 1, xlR1C1)

[/SIZE][SIZE=1][COLOR=seagreen]'MsgBox straddr[/COLOR][/SIZE]
[SIZE=1]    Set xlshp = xlw.Worksheets("ProvPT")
    
 For Each pt In xlshp.PivotTables
    [COLOR=seagreen]'Sheets(1).PivotTables(1).PivotCache.SourceData = "'" & Sheets(2).Name & "'!" & Range("A5:T40000").Address
    'Sheets(1).PivotTables(1).PivotCache.SourceData = Sheets(2).Name & "!" & Range("A5:T40000").Address(ReferenceStyle:=xlR1C1)
    
[/COLOR]    Set ptc = pt.PivotCache
    
   [COLOR=seagreen] 'ptc.SourceData = Worksheets("ProvList")!Range("A1:C5000").Address(ReferenceStyle:=xlR1C1)
    'ptc.SourceData = Worksheets(3).Name & "!" & Range("A1:C5000").Address(ReferenceStyle:=xlR1C1)
    'ptc.SourceData = xlsh.UsedRange.Address(1, 1, xlR1C1)
    'ptc.SourceData = "'ProvList'!" & Range("$A:$C").Address
    'ptc.SourceData = rng.Address(1, 1, xlR1C1)
    'ptc.SourceData = rng.CurrentRegion.Address
    
     'xlshp.PivotTables(1).PivotCache.SourceData = xlsh.UsedRange.Address
     'xlshp.PivotTables(1).PivotCache.SourceData = "'ProvList'!" & Range("$A:$C").Address
     'xlshp.PivotTables(1).PivotCache.SourceData = xlsh.UsedRange.Address(1, 1, xlR1C1)
     'xlshp.PivotTables(1).PivotCache.SourceData = xlsh.UsedRange.Address(1, 1, xlR1C1)
     'xlshp.PivotTables(1).PivotCache.SourceData = xlsh.UsedRange.Address(True, True, xlR1C1)
     'xlshp.PivotTables(1).PivotCache.SourceData = xlw.Worksheets("ProvList") & "!" & Range("A1:C5000").Address(1, 1, xlR1C1)
     'xlshp.PivotTables(1).PivotCache.SourceData = xlw.Worksheets("ProvList") & "!" & Range("A1:C5000").Address(ReferenceStyle:=xlR1C1)[/COLOR][/SIZE]
[SIZE=1][COLOR=seagreen]    'MsgBox "The data source connection is: " & xlw.PivotCaches.Item(1).SourceDataFile
    '    Set ptc = xlw.PivotCaches.Item(1)
    '    MsgBox "The data source connection is: " & ptc.SourceDataFile[/COLOR][/SIZE]
[SIZE=1][COLOR=seagreen][/COLOR][/SIZE] 
[SIZE=1][COLOR=seagreen][COLOR=#008000] 'xlshp.PivotTableWizard SourceType:=xlPivotTable, SourceData:=straddr[/COLOR]

[/COLOR]    
  [COLOR=red] ptc.SourceData = straddr
[/COLOR]    
[COLOR=green]  [/COLOR][/SIZE]
[SIZE=1]    pt.RefreshTable
    pt.Update
    
     
 Next
   
End With[/SIZE]
[SIZE=1]xlw.Save[/SIZE]
[SIZE=1]Exit_FormatSpreadsheets_Click:
Set xl = Nothing
xlw.Close
Set xlw = Nothing
Set xlsh = Nothing
Set xlshp = Nothing[/SIZE]
[SIZE=1]    Exit Sub[/SIZE]
[SIZE=1]Err_FormatSpreadsheets_Click:
    MsgBox Err.Description
    Resume Exit_FormatSpreadsheets_Click[/SIZE]
[SIZE=1]End Sub[/SIZE]
[SIZE=1]
[/SIZE]
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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