Runtime Error 1004 on Macro

Mikeykt

New Member
Joined
Feb 10, 2011
Messages
47
Hi

I keep getting a Runtime Error 1004 on the following Macro,

It keeps failing at the highlighted area. Anybody have any ideas?

Thanks

Dim wksPivot As Worksheet
Dim wksData As Worksheet
Dim pc As PivotCache
Dim PT As PivotTable
Set wksPivot = Sheets("RSPIVOT")
Set wksData = Sheets("Inactive RG by Recruit Type 2")
wksPivot.UsedRange.Clear
With wksData
Set pc = .Parent.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"'" & .Name & "'!" & .Range("A1").CurrentRegion.Address(0, 0, xlR1C1))
Set PT = pc.CreatePivotTable(TableDestination:=wksPivot.Cells(3, 1), TableName:="")
End With
With PT
With .PivotFields("Behaviour")
.Orientation = xlPageField
.Position = 1
End With
With .PivotFields("Value")
.Orientation = xlPageField
.Position = 1
End With
With .PivotFields("Recency")
.Orientation = xlPageField
.Position = 1
End With
With .PivotFields("Segment")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Recruitment Summary")
.Orientation = xlColumnField
.Position = 1
End With
With .PivotFields("Recruitment Source")
.Orientation = xlColumnField
.Position = 2
End With
.AddDataField .PivotFields("No Supporters"), "Count of No Supporters", xlCount
With .PivotFields("Count of No Supporters")
.Caption = "Sum of No Supporters"
.Function = xlSum
End With
.Name = "RSPN"
End With
End Sub
 
I thought you said your code was CommandButton code? Or are you using a button from the Forms Toolbar and assigning it that macro?

What are the headings in row 1 of your data worksheet? Is the CurrentRegion what you would expect?
 
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You're right I'm using a button from the Form Control and not the Command Button.

Can I still assign the macro to the Form Control button or do i have to add more code?
 
Upvote 0
I don't have Excel 2007 where I am at the moment so I can't test until I get home. Are you sure that you have valid headings in row 1?
 
Upvote 0
Yes,

The macro works when I'm on the 'Inactive RG by Recruitment Type 2' worksheet or the 'RSPIVOT' worksheet, but not any other.

So the 1st line rows have to be correct.

Is there anything in the code that would only allow it to work on these two worksheets only?
 
Upvote 0
This worked for me when assigned to a CommandButton from the Forms Toolbar on a different worksheet:

Rich (BB code):
Sub RSPivotNum()
'
' RSPivotNum Macro
'
'
    Dim wksPivot As Worksheet
    Dim wksData As Worksheet
    Dim pc As PivotCache
    Dim PT As PivotTable
    Set wksPivot = Sheets("RSPIVOT")
    Set wksData = Sheets("Inactive RG by Recruit Type 2")
    wksPivot.UsedRange.Clear
    With wksData
        Set pc = .Parent.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
            "'" & .Name & "'!" & .Range("A1").CurrentRegion.Address(True, True, xlR1C1))
        Set PT = pc.CreatePivotTable(TableDestination:="'" & wksPivot.Name & "'!R3C1", TableName:="")
    End With
    With PT
        With .PivotFields("Behaviour")
            .Orientation = xlPageField
            .Position = 1
        End With
        With .PivotFields("Value")
            .Orientation = xlPageField
            .Position = 1
        End With
        With .PivotFields("Recency")
            .Orientation = xlPageField
            .Position = 1
        End With
        With .PivotFields("Segment")
            .Orientation = xlRowField
            .Position = 1
        End With
        With .PivotFields("Recruitment Summary")
            .Orientation = xlColumnField
            .Position = 1
        End With
        With .PivotFields("Recruitment Source")
            .Orientation = xlColumnField
            .Position = 2
        End With
        .AddDataField .PivotFields("No Supporters"), "Count of No Supporters", xlCount
        With .PivotFields("Count of No Supporters")
            .Caption = "Sum of No Supporters"
            .Function = xlSum
        End With
        .Name = "RSPN"
    End With
End Sub

Note that I changed the first 2 arguments of the Address property from 0 to True.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,713
Members
452,939
Latest member
WCrawford

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