mit nachstehendem Code wird eine UF mit 2 CommandButton und Click-Code für die CBs erstellt.
Klappt auch bestens. Die erzeugten Codes sehen so aus:
Private Sub CB1_Click()
MsgBox "Hallo, ich bin CB"
End Sub
Private Sub CB2_Click()
MsgBox "Hallo, ich bin CB"
End Sub
Ich hätte es aber gerne so:
Private Sub CB1_Click()
MsgBox "Hallo, ich bin CB "
End Sub
Private Sub CB2_Click()
MsgBox "Hallo, ich bin CB "
End Sub
Was muß ich an der Codezeile:
.InsertLines intRow + 1, "MsgBox ""Hallo, ich bin CB"""
abändern? Mit gelang es nicht die Variable j dort korrekt einzubasteln.
Danke ^ Gruß
Reinhard
Option Explicit
Sub UserForm()
Dim frmCommandButton, VBComp, strName
Dim intRow, jj, j, S, T, myCommand
jj = 2
Set VBComp = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
With VBComp
.Properties("Caption") = "Meine Userform"
.Properties("Width") = 400
.Properties("Height") = 500
strName = .Properties("Name")
End With
For j = 1 To jj
S = "CB" & j
Set myCommand = VBComp.Designer.Controls.Add("Forms.commandbutton.1", S)
With myCommand
.Caption = "Write!" & j
.Left = 0
.Top = T
.Height = 60
.Width = 40
End With
T = T + 70
With VBComp.CodeModule
intRow = .CreateEventProc("Click", S)
.InsertLines intRow + 1, "MsgBox ""Hallo, ich bin CB"""
End With
Next j
VBA.UserForms.Add(strName).Show
End Sub