Macros in Pivot Tables - Can you change the data source and the macro still work?

amr4434

New Member
Joined
Jul 27, 2011
Messages
4
I have two very simple macros in a spreadsheet (code copied below). I linked the macros to buttons for end users to change the data to a percent and back to a total number (see image below). I created this last year and want to replace the 'source data' with current data. I did not want to start over building the macros. Is it possible to change the data source and make sure the macros will still work? I tried on my own and it didn't work - which is why I am here. Also - I know zero about writing code and anything related to VBA. Simplistic vocabulary replies are appreciated.

Picture1.jpg


For reference, here is an example of one of the macro codes (it is for a different worksheet - but the basics are the same):

Sub m_lev_grd_p()
'
' m_lev_grd_p Macro
'

'
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of Math Level")
.Calculation = xlPercentOfRow
.NumberFormat = "0.00%"
End With
End Sub
Sub m_lev_grd_t()
'
' m_lev_grd_t Macro
'

'
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of Math Level")
.Calculation = xlNormal
.NumberFormat = "General"
End With
End Sub

moz-screenshot.png
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
It may help to note the error message I received...

After changing the data source and clicking on the button/macro - I get:

"run-time error '1004' unable to get the pivotfields property of the pivottable class"
 
Upvote 0

Forum statistics

Threads
1,224,567
Messages
6,179,571
Members
452,927
Latest member
whitfieldcraig

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