how to add Macro to a button

PaulaO

New Member
Joined
Jul 2, 2012
Messages
40
this may be a very easy question,

I have the following macro:

Private Sub Worksheet_Activate()
With ActiveSheet
.PivotTables("PivotTable1").RefreshTable
.PivotTables("PivotTable2").RefreshTable
.PivotTables("PivotTable3").RefreshTable
.PivotTables("PivotTable4").RefreshTable
End With
End Sub



This macro runs automatically when i click on the sheet.
Instead, I want to add a button in this sheet that runs this macro.


How do I do this?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Assign this to a button

Code:
Sub test()
With ActiveSheet
    .PivotTables("PivotTable1").RefreshTable
    .PivotTables("PivotTable2").RefreshTable
    .PivotTables("PivotTable3").RefreshTable
    .PivotTables("PivotTable4").RefreshTable
End With
End Sub
 
Upvote 0
do i insert a form control button?

my code ends up like this
Sub Button1_Click()
Sub test()
With ActiveSheet
.PivotTables("PivotTable1").RefreshTable
.PivotTables("PivotTable2").RefreshTable
.PivotTables("PivotTable3").RefreshTable
.PivotTables("PivotTable4").RefreshTable
End With
End Sub

and it doesn't work.

i gues smy challenge is actually filling out the correct fields when adding a button
 
Upvote 0
With a Forms button

Code:
Sub Button1_Click()
With Me
    .PivotTables("PivotTable1").RefreshTable
    .PivotTables("PivotTable2").RefreshTable
    .PivotTables("PivotTable3").RefreshTable
    .PivotTables("PivotTable4").RefreshTable
End With
End Sub
 
Upvote 0
thanks that works like a charm,

how do i add the code below to that button? can I just paste it after the first part?

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "H2" Then Me.Name = Target.Value
End Sub
 
Upvote 0
Try

Code:
Sub Button1_Click()
With ActiveSheet
    .PivotTables("PivotTable1").RefreshTable
    .PivotTables("PivotTable2").RefreshTable
    .PivotTables("PivotTable3").RefreshTable
    .PivotTables("PivotTable4").RefreshTable
    .Name = .Range("H2").Value
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,559
Messages
6,114,302
Members
448,564
Latest member
ED38

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