Pivot table

MJA

Board Regular
Joined
Feb 18, 2002
Messages
79
Hello,

Sub Macro1()
Range("F2").Select
ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _
"MARTIN!R2C1:R155C8"
End Sub

Above stands a part of a macro to create a pivot table. With the following I have a problem:
SourceData:="MARTIN!R2C1:R155C8"

If the sheet has a name other then MARTIN I get an error.
I want to play the macro whatever the sheet name is!
How can I make that happem?!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
If you want it to appear on the active sheet, use the following line instead of the one containing MARTIN!, you'll need to make sure of the range too though, otherwise you'll get an error: -

ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _
"" & ActiveSheet.Name & "!" & "R2C1:R155C8"
 
Upvote 0
Actually, thinking about it, the following should work for any sheet which has listed data in column A, (change the Range("A1").Select statement to reflect your data position): -

Sub Macro1()
Dim myRange as Range

Range("A1").Select
Set myRange = ActiveCell.CurrentRegion
ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _
"" & ActiveSheet.Name & "!" & myRange.Address(ReferenceStyle:=xlR1C1) & ""
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,375
Messages
6,171,707
Members
452,418
Latest member
kennettz

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