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?
 

Some videos you may like

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 

PaulaO

New Member
Joined
Jul 2, 2012
Messages
40
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
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 

PaulaO

New Member
Joined
Jul 2, 2012
Messages
40

ADVERTISEMENT

I get an error message ' Invalid use of Me keyword'
 

PaulaO

New Member
Joined
Jul 2, 2012
Messages
40

ADVERTISEMENT

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
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,469
Messages
5,596,329
Members
414,055
Latest member
mcarduner

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
Top