PC Review


Reply
Thread Tools Rate Thread

HOW CAN I WRITE A CODE TO COMMANDBUTTON ADDED AT RUNTIME?

 
 
Kozete
Guest
Posts: n/a
 
      6th Jun 2008
After I add a CommandButton control to a UserForm by Add method, how can I
write a code to that CommandButton?

--
Kozete
 
Reply With Quote
 
 
 
 
JLGWhiz
Guest
Posts: n/a
 
      6th Jun 2008
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

 
Reply With Quote
 
Tim Zych
Guest
Posts: n/a
 
      6th Jun 2008
You could theoretically write code to write code, but you will probably run
into problems quickly (virus detection may think it's a virus and
quarantines the workbook. The code persists, so it needs to be cleaned up
and rebuilt. The macro is reformed as a string, making edits a pain).

John Walkenbach has a nifty macro to do this using a class, avoiding the
problems above.

http://www.j-walk.com/ss/excel/tips/tip44.htm

To delve into this more, if you have access to it, check out Francesco
Balena's Programming Visual Basic 6. Balena's one of the best authors out
there and he has a chapter in his book devoted dynamically created forms and
events.


--
Tim Zych
www.higherdata.com
Compare data in worksheets and find differences with Workbook Compare
A free, powerful, flexible Excel utility


"Kozete" <(E-Mail Removed)> wrote in message
news:9881583F-DB83-4E54-821B-(E-Mail Removed)...
> After I add a CommandButton control to a UserForm by Add method, how can I
> write a code to that CommandButton?
>
> --
> Kozete



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HOW CAN I WRITE A CODE TO A COMMANDBUTTON CREATED PROGRAMMATICALLY Kozete Microsoft Excel Programming 1 11th Jun 2008 09:37 PM
WorkSheet_Change code from a CommandButton =?Utf-8?B?TWFyayBEdWxsaW5naGFt?= Microsoft Excel Programming 4 30th Mar 2007 12:52 PM
CommandButton code help peter.thompson Microsoft Excel Programming 3 23rd Jan 2006 01:32 AM
? activate a commandbutton in code ? tad_wegner Microsoft Excel Programming 1 10th Oct 2005 04:43 PM
commandbutton code =?Utf-8?B?amVmZg==?= Microsoft Excel Misc 1 7th Aug 2004 08:54 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:46 PM.