View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Jag Man Jag Man is offline
external usenet poster
 
Posts: 38
Default Creating Sub CommandButton1_Clk programmatically

Hi Tom,

That's strange. I am indeed a VBA novice, so I am inclined to believe you,
but that's not what I'm
observing. If you don't mond, let me explore this a bit further with you.

Here's what I see. From the VBA editor, in a new Module in my project I
place the code:

Public Sub myNewWorksheet()
' Create the Worksheet
Dim WSName As String
WSName = "testIt"
ActiveWorkbook.Sheets.Add Type:=xlWorksheet
ActiveWorkbook.ActiveSheet.name = WSName
Dim WS As Worksheet
Dim Btn As OLEObject
Set WS = ActiveWorkbook.ActiveSheet
' Create The Button
Set Btn = WS.OLEObjects.Add(ClassType:="Forms.CommandButton. 1", _
Left:=WS.Range("D3").Left, Top:=WS.Range("D3").Top, _
Width:=95, Height:=40)
Btn.Object.Caption = "Calculate"
Btn.name = "TheButton"
End Sub

I then switch over to Excel and hit alt-f8 to get the list of public macros.
There I see myNewWorksheet, which
I select and click on Run. The new worksheet appears, with button in place.
I do not touch the button.

I then switch back to the VBA editor. Under Microsoft Excel Objects in the
tree view I see Sheet5(testIt). I click on that,
causing a window to open in the right had side. In the selection menu at the
top left it says (General), and the
edit panel is empty. But when I select TheButton in the selection menu at
the top, there appears in the edit panel
the header for the Click event, and the End Sub. If the programmatic
creation of the button did not
create this, what did? Is it the editor?

Thanks for your patience.

Ed

"Tom Ogilvy" wrote in message
...
Well, I tested it to be sure, and adding a button programmatically does

not
create the sub header and end sub lines (why would it - this would be an
undesirable side effect). Perhaps you double clicked on the control after
adding it programmatically to look at the code module. This does create

the
click event - but adding the control does not.

--