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.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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
 
Upvote 0

Forum statistics

Threads
1,215,052
Messages
6,122,878
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top