how to use the active commandbutton to execute the codes

xuniren2008

New Member
Joined
Oct 15, 2006
Messages
1
The VBA programme allow
us to add active commandbutton and other label at the running time.But
how to use the active commandbutton to execute the codes,not just use its properties,but its method.

look the following example:
dim picCount as integer
picCount=0
me.height=int(0.98*activewindows.height)
me.width=int(0.98*activewindows.width)
LC="labelA"&picCount
me.controls.add bstrProgId:="forms.label.1",Name:=LC,visible=true
me.controls(LC).top=25
me.controls(LC).left=50

When we want to add bstrProgId:="forms.commandbutton.1",
How to let this commandbutton to execute some codes,not just use its propertities.
 

Some videos you may like

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
L

Legacy 98055

Guest
Using events of dynamically added controls

See the WithEvents keyword in VBA help. Also, see the download for an example using the following code.

Using events of dynamically added controls.zip

Code entered into a userform containing commandbutton1.
<table width="100%" border="1" bgcolor="White" style="filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');"><tr><TD><font size="2" face=Courier New>  <font color="#0000A0">Option</font> <font color="#0000A0">Explicit</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">WithEvents</font> l <font color="#0000A0">As</font> MSForms.CommandButton

  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> CommandButton1_Click()
       <font color="#0000A0">Set</font> l = Me.Controls.Add(bstrProgId:="forms.commandbutton.1", Name:="LC", Visible:=True)
       l.Top = 5
       l.Left = 5
       l.Caption = "Click HERE on your dynamically added commandbutton"
       l.AutoSize = <font color="#0000A0">True</font>
       l.WordWrap = <font color="#0000A0">True</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> l_Click()
       MsgBox "You clicked on your dynamically added commandbutton..."
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
</FONT></td></tr></table><button onclick='document.all("101520061225515").value=document.all("101520061225515").value.replace(/<br \/>\s\s/g,"");document.all("101520061225515").value=document.all("101520061225515").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("101520061225515").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="101520061225515" wrap="virtual">
Option Explicit

Private WithEvents l As MSForms.CommandButton

Private Sub CommandButton1_Click()
Set l = Me.Controls.Add(bstrProgId:="forms.commandbutton.1", Name:="LC", Visible:=True)
l.Top = 5
l.Left = 5
l.Caption = "Click HERE on your dynamically added commandbutton"
l.AutoSize = True
l.WordWrap = True
End Sub

Private Sub l_Click()
MsgBox "You clicked on your dynamically added commandbutton..."
End Sub</textarea>

Using events of dynamically added controls.zip
 

Watch MrExcel Video

Forum statistics

Threads
1,114,059
Messages
5,545,758
Members
410,704
Latest member
Cobber2008
Top