Creating a Button that runs a Macro Using VBA

aryaden

Board Regular
Joined
Jun 9, 2021
Messages
101
Office Version
  1. 2019
Platform
  1. Windows
I would like to create a macro that creates a button to run another macro in a specific sheet in a workbook. I have multiple workbooks with a worksheet (same title in all workbooks) that i need to place a "run macro" button in cell B2 for. The button when clicked would run another macro I have already created.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,675
Office Version
  1. 2013
Platform
  1. Windows
I would suggest creating a Macro that does what you want.
Put that Macro in your Personal Workbook.
And activate the script by using a shortcut Key.
This script would run on any workbook you have open.
No need having a Button to run the script.
 

aryaden

Board Regular
Joined
Jun 9, 2021
Messages
101
Office Version
  1. 2019
Platform
  1. Windows
I would suggest creating a Macro that does what you want.
Put that Macro in your Personal Workbook.
And activate the script by using a shortcut Key.
This script would run on any workbook you have open.
No need having a Button to run the script.
Thanks for the suggestion but after I make changes I am sending the workbook to a client. I wanted them to be able to make changes and run my macro if needed by clicking a button.
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,068
You could loop through the sheets with code like this
VBA Code:
With ActiveSheet
    With .Buttons.Add(184, 57, 120, 30)
        .Top = .Parent.Range("B2").Top
        .Left = .Parent.Range("B2").Left
        .OnAction = "myOtherMacro"
    End With
End With
 
Solution

aryaden

Board Regular
Joined
Jun 9, 2021
Messages
101
Office Version
  1. 2019
Platform
  1. Windows

ADVERTISEMENT

You could loop through the sheets with code like this
VBA Code:
With ActiveSheet
    With .Buttons.Add(184, 57, 120, 30)
        .Top = .Parent.Range("B2").Top
        .Left = .Parent.Range("B2").Left
        .OnAction = "myOtherMacro"
    End With
End With
Thank you! I will try that, if I wanted to apply this to a specific worksheet named "Data_1" how would I change the code?
 

Forum statistics

Threads
1,141,125
Messages
5,704,440
Members
421,349
Latest member
Santhosh3188

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