Using VBA to copy a pivottable

David31101990

New Member
Joined
Nov 3, 2009
Messages
4
Hello,

I wanted to use a VBA function that is able to copy a pivottable which is on a server into my workbook. This is the VBA i'm using now.

Code:
Sub CopyPvts()
Dim ws As Worksheet
Dim wb As Workbook
Dim SheetList
Dim i As Long
SheetList = Array("A", "B") ' change to suit - these are the sheet names to copy TO and must exist
Set wb = Workbooks.Open("C:\Users\David\Documents\Afstudeerstage\DraaiTabelGebruikAfprijzing2007.xlsx") ' change to suit
For Each ws In Worksheets(Array("AfprPerFilPerSubgrpPerArtGELD", "AfprPerFilPerSubgrpPerArtCE")) ' change to suit = sheet names to copy FROM
    ThisWorkbook.Sheets(SheetList(i)).ClearContents
    ws.UsedRange.Copy Destination:=ThisWorkbook.Sheets(SheetList(i)).Range("A1")
    i = i + 1
Next ws
wb.Close savechanges:=False
End Sub

When I run the macro, I get "Run-time error '438': Object doesn't support this property or method". When I click 'Debug', "ThisWorkbook.Sheets(SheetList(i)).ClearContents" is highligted. What am I doing wrong? I changed the names to suit so that isn't the problem.

Thanks

David
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Forum statistics

Threads
1,203,026
Messages
6,053,113
Members
444,639
Latest member
xRockox

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