I am working on a code that has two command buttons:
1) USER INPUT
2) Execute
When clicked, the USER INPUT button makes a UserForm appear. Based on the UserForm input, the worksheet format adjusts and the user inputs data to the worksheet where prompted. The Execute button performs calculations and fills out the rest of the sheet and then graphs the results OR opens a new sheet that then contains the same two buttons.
I am able to create the new sheet but the sheet only contains one command button. My code is below:
Dim obj As Object
Dim Code As String
Dim obj2 As Object
Dim code2 As String
With Sec_Delay
Set obj = .OLEObjects.Add(classType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, Left:=279, _
Top:=210.75, Width:=109.5, Height:=24)
obj.Name = "ButtonTest"
obj.Object.Caption = "USER INPUT"
Code = "Sub ButtonTest_Click()" & vbCrLf & _
"UF_input.Show" & vbCrLf & _
"End Sub"
Set obj2 = .OLEObjects.Add(classType:="Forms.CommandButton.2", _
Link:=False, DisplayAsIcon:=False, Left:=277.5, _
Top:=236.25, Width:=111, Height:=24)
obj2.Name = "Execute2Test"
obj2.Object.Caption = "Execute"
code2 = "Sub Execute2Test_Click()" & vbCrLf & _
"Cells(8,1) = 1" & vbCrLf & _
"End Sub"
With .Parent.VBProject.VBComponents(.CodeName).CodeModule
.insertlines .CountOfLines + 1, Code
End With
End With
This code is within my sub that creates the new sheet. The new sheet is called "Sec_Delay" and it only has one command button. I lifted the code for the first command button from somewhere else on stackoverflow so I am not familiar with whatever the last part does:
With .Parent.VBProject.VBComponents(.CodeName).CodeModule
.insertlines .CountOfLines + 1, Code
End With
But I understand mostly how the OLEObject works. I just don't get how to make a second command button for the new sheet.
How can I create a second command button? Why does "Forms.CommandButton.2" not do anything? What does the ".1" mean anyways? Is it possible to have two OLEObjects in the same sub?
You're almost there...
"Forms.CommandButton.1" is the classname for the control: it determines what type of control gets created, and you shouldn't alter the value or excel won't recognize it.
The last part of the code adds event handlers for the buttons to the sheet's code module: after you create the sheet you can view the code in the VB editor.
Dim obj As Object
Dim Code As String
Dim obj2 As Object
Dim code2 As String
With Sec_Delay
Set obj = .OLEObjects.Add(classType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, Left:=279, _
Top:=210.75, Width:=109.5, Height:=24)
obj.Name = "ButtonTest"
obj.Object.Caption = "USER INPUT"
Code = "Sub ButtonTest_Click()" & vbCrLf & _
"UF_input.Show" & vbCrLf & _
"End Sub"
'edit: use "Forms.CommandButton.1" again
Set obj2 = .OLEObjects.Add(classType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, Left:=277.5, _
Top:=236.25, Width:=111, Height:=24)
obj2.Name = "Execute2Test"
obj2.Object.Caption = "Execute"
code2 = "Sub Execute2Test_Click()" & vbCrLf & _
"Cells(8,1) = 1" & vbCrLf & _
"End Sub"
With .Parent.VBProject.VBComponents(.CodeName).CodeModule
.insertlines .CountOfLines + 1, Code
.insertlines .CountOfLines + 1, code2 '<< added
End With
End With
EDIT: if you want to call code which is located in a sheet module from a regular module, you need to include the module name in the call.
Sub TestCall()
Sheet1.Tester
End Sub
...and make sure you're using the sheet's codename, not the tab name: