PDA

Click to See Complete Forum and Search --> : Command Button


gazza52_2000
Dec 21st, 2005, 03:54 AM
I've got a form which contains a command button.

On my excel spreadsheet i've got another command button which calls the form.

What i want to do is when i click the command button on the spreadsheet it calls the form and then runs the command button code. (Just like clicking the command button on the form)

Please help!!v :bigyello:

Thanks

Andrew G
Dec 21st, 2005, 03:59 AM
Wrong forum, you need the office development (http://www.vbforums.com/forumdisplay.php?f=37) forum

Bruce Fox
Dec 21st, 2005, 04:03 AM
In the Excel CommandButton click event, do something like:

FormName.Show

That will load and show the UserForm.

Then, in the UserForm_Activate event, call the CommandButton click event, like:

Call CommandButtonX_Click()


It isn't the best practice calling a Controls Click (or other) event, so what I would suggest
is creating a Sub (or Function if required) and call that from the Forms CommandButton click event,
and also call it from the UserForm Activate event (and not directly, such as Call CommandButtonX_Click()).

gazza52_2000
Dec 21st, 2005, 05:11 AM
Bruce

I've got the form to show up ok.

I'm confused as to where to place the Call CommandButtonX_Click().

You say to add this to the UserForm_Activate event. I do not know where i can find this?. Is this the forms code? If so where would i place this command within the code?.

Thanks again :thumb:

RobDog888
Dec 21st, 2005, 05:24 AM
Moved from Classic VB forum.

RobDog888
Dec 21st, 2005, 05:29 AM
In either your Sheet code or Workbook code you have your sheets commandbutton code to show the userform. After you click it behind your userform code you will have available to you the UserForm_Activate event procedure. This is where you could call the commandbutton click event but if you switch between the userform and the spreadsheet back and forth then the code for the command button will be run each and every time it "Activates" the userform.
Private Sub UserForm_Activate()
Call CommandButton1_Click
End Sub