View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
JLGWhiz JLGWhiz is offline
external usenet poster
 
Posts: 3,986
Default HOW CAN I WRITE A CODE TO COMMANDBUTTON ADDED AT RUNTIME?

Command buttons from the forms toolbar are attached to the macro through an
assignment dialog box at the time the marcro is created, or by calling the
dialog box at a later time, if required. These cannot be easily connected
with VBA.

Command buttons from the Control Tool box have a click event code. There
are two methods to tie the macro to the button click event. One is to put
the code directly into the click event by right clicking the button in design
mode and then click view code. This will automatically display the first and
last line of the click event macro and the user can fill in the middle part.
The second method is to put the executable code in the standard project code
module and just use the click event to call that code. For example your code
in the standard module is Macro1, this would be the code behind the command
button:

Private Sub CommandButton1_Click()
Macro1
End Sub

Macro1 would then execute from the standard module. But in any case, The
code to enact the command button must be in the command button attributes.



"Kozete" wrote:

After I add a CommandButton control to a UserForm by Add method, how can I
write a code to that CommandButton?

--
Kozete