View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
FSt1 FSt1 is offline
external usenet poster
 
Posts: 3,942
Default Programatically click CommandButton

hi
that is what i thought you were trying to do. follow Leith's suggestion
instead.

Regards
FSt1

"donwb" wrote:

Hi FSt1
I have a UserForm with many CBs.
I need a macro (lets call it Macro1) to programmatically click
CB1 on the UF, which will then do something.
UserForm1.CommandButton1_Click in macro1 does this ok.
When that's done, control goes back to macro1, which I then want to click
CB2.
UserForm1.CommandButton2_Click would do this.
But there are 20 or so CBs to click,
so I wanted to automate the process with something like
UserForm1.CommandButton(X)_Click, with values 1 to 20
assigned to the variable X sequentially.
The real question is:-
can UserForm1.CommandButton(X)_Click
become a named procedure when X is given a number
- with the correct syntax?
or is it treated as a named procedure in VBA
ONLY if is assigned an integer and not a variable?
donwb

"FSt1" wrote in message
...
hi
incorrect syntax.
what are you trying to pass to the procedure?
variables can be passed but the call command is looking for a named
procedure and what you have written is not a named prodedure.
UserForm1.CommandButton1_Click is your named prodedure.
UserForm1.CommandButton(X)_Click is not.
the correct syntax would be.....
call UserForm1.CommandButton1_Click ()
you can pass variables, arrays or expressions but they have to be within
the
parenthases. but i'm not too sure about a button number. what are you
trying
to do...call 5 different button click?
regards
FSt1
"donwb" wrote:

The following expression works fine and clicks CommandButton #1:-
Call UserForm1.CommandButton1_Click

Is it possible to assign a variable to the number of the button clicked,
along the lines:-

For X=1 to 5 step 1
Call UserForm1.CommandButton(X)_Click
(Then does what clicking the CB does)
Next X

This doesn't work, but I don't know if the fault is incorrect syntax,
or that the expression simply cannot take a variable.
donwb