How to auto-click a button on a userform in VBA

arskiracer

New Member
Joined
Apr 3, 2013
Messages
18
Hello, I'm attempting to have a checkbox option in a userform that will allow the macro to execute without requiring the user to click the "Run" button to continue. I'm having a hard time making the code jump from the end of the "UserForm_Initialize()" sub into the "RunButton_Click()" sub, where it would normally go once the user clicked on the run button.

Does anyone know how to make this happen?

After some Google searches, I tried the following code inside the userform_initialize section (at the very end) without success:

RunButton_Click

SettingsForm.RunButton_Click

Call SettingsForm.RunButton_Click

'these two together:
RunButton = True
RunButton = vbClick

I don't get any errors from any of this, it just doesn't seem to do anything. [correction: I do get an error on SettingsForm.RunButton_Click for some reason]


I never intended to have this macro run automatically, so I put a lot of code in the "RunButton_Click()" area in order to save userform data for future macro executions (and I don't want to redo that code), but now I'm repurposing this for something else and would like the option to have it run automatically without user interaction.
 
Last edited:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You can move the code out ofRunButton_Click event procedure and into your own procedure. Then you can call this procedure at the end of UserForm_Initialize if the checkbox is checked.

So
Code:
UserForm_Initialize()
' Initialization code
End Sub

RunButton_Click()
' Run process operation code here
End Sub

becomes
Code:
UserForm_Initialize()
' Initialization code
    ' Call MyProcess if checkbox is checked
    If CheckBox1.Value Then MyProcess
End Sub

RunButton_Click()
    MyProcess
End Sub

Sub MyProcess()
' Run process operation code here
End Sub
 
Upvote 0
That is a great idea, I'll give it a shot. And thanks for the explanation, don't know if I would have understood what you were saying without the examples.
 
Upvote 0

Forum statistics

Threads
1,222,098
Messages
6,163,927
Members
451,866
Latest member
cradd64

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