View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Control Click Subroutine (BHatMJ)

Here's some code that does one commandbutton:

Option Explicit
Sub testme()

Dim OLEObj As OLEObject
Dim wks As Worksheet

Set wks = ActiveSheet

With wks
With .Range("a1:b2")
Set OLEObj = .Parent.OLEObjects.Add _
(ClassType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, _
Left:=.Left, Top:=.Top, _
Width:=.Width, Height:=.Height)
End With

With .Parent.VBProject.VBComponents(.CodeName).CodeModu le
.InsertLines .CreateEventProc("Click", OLEObj.Name) + 1, _
"Msgbox ""Hi"""
End With
End With

End Sub

=====
I'm not sure how married you are to the idea of using commandbuttons from the
control toolbox toolbar, but you may want to consider using buttons from the
Forms toolbar.

Then you can use the same macro (in a general module) for each button. Your
code would have to figure out what button called it, though (depending on what
your subroutine did).





BHatMJ wrote:

HELP! I am adding a dynamic number of control buttons to a worksheet
dependent upon user input during run time (see my code below). I need to add
code to each of these buttons (also during run time) so that a subroutine is
called when the user selects the button.

ANY help would be greatly appreciated!!!

Sub AddCtrl(nCnt As Integer)

topPos = 20
' nCnt is an integer set at run time by the user
For lp = 1 to nCnt
ActiveSheet.OLEObjects.Add(ClassType:="Forms.Comma ndButton.1", Left:=100,
Top:=topPos, Width:=50, Height:= 30).Select
topPos = topPos + 75
Next lp

End Sub


--

Dave Peterson