VBA to click button of another userform?

ShieBoon

Board Regular
Joined
May 3, 2011
Messages
111
Hi all,

I have two userforms open at the same time. And i want a particular code of the active userform to

First, unload itself. (Code: Unload Userform1)
Second, click a button in the second userform. (Userform2)
(Code: button1_Click)

But an error prompts, "Sub or Function not defined" when i try to run the entire code. 'Button1_Click' is highlighted. I tried changing button1's click event from private to public but still unable to work.

Any ideas?

excel2003
Shie Boon
Thanks
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
What does the click event on the other userform do?

Have you considered moving it's code to a separate sub?

Then you could call that sub from the click event of the form it's on and from the other form.
 
Upvote 0
Hi Norie, thanks for the reply. I found out the problem already though, i added the other userform's name infront of the code.
userform2.button1_Click
It was weird though because it didn't work yesterday. Today it did.
 
Upvote 0
I had the same issue until I realized that by default, the methods (Sub) on a form are of type "Private"... No matter how you call the methods on the other user form, it won't work until you change the word "Private" to "Public"

frmName.SubName, ie frmUser1.cboStates_Change

And make sure the Sub on the frmUser1 for the cboStates_Change is "Public". Now this sub can be called from any other object in the workbook.

Et Voilà!
 
Upvote 0

Forum statistics

Threads
1,216,775
Messages
6,132,655
Members
449,743
Latest member
rakeshsanger

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