Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default HOW CAN I WRITE A CODE TO COMMANDBUTTON ADDED AT RUNTIME?

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

--
Kozete
  #2   Report Post  
Posted to microsoft.public.excel.programming
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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 389
Default HOW CAN I WRITE A CODE TO COMMANDBUTTON ADDED AT RUNTIME?

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" wrote in message
...
After I add a CommandButton control to a UserForm by Add method, how can I
write a code to that CommandButton?

--
Kozete



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Textbox added to Form at runtime too small, can't control size andchange event won't trigger RCGUA Excel Worksheet Functions 2 December 10th 08 09:34 PM
Click events not working when added during runtime crazybass2 Excel Programming 3 July 10th 06 04:06 PM
Manipulate Controls added at runtime [email protected] Excel Programming 5 January 17th 06 09:50 AM
MatchEntry for combobox added during runtime Claus[_3_] Excel Programming 6 October 13th 05 12:43 PM
add event to controls added in runtime Brotha lee Excel Programming 1 May 21st 05 10:32 AM


All times are GMT +1. The time now is 09:52 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"