Ich habe einen Code für ein Benutzerformular mit einer Listbox, aber wenn ich das Benutzerformular die Listbox zeige ist leer, aber wenn ich nicht zeige, kann ich die Gegenstände sehen.Ich habe ein Benutzerformular mit einer Listbox, aber wenn ich das Benutzerformular zeige, ist das Listenfeld leer, aber wenn ich es nicht zeige, kann ich die Elemente sehen
Option Explicit
Dim Tableau(0) As String
Dim myForm As Object ' or use 'As VBComponent'
Dim Ctrl As Control
Dim NewButton1 As MSForms.CommandButton
Dim NewButton2 As MSForms.CommandButton
Sub erstellen()
Tableau(0) = "1"
Set myForm = ThisWorkbook.VBProject.VBComponents.Add(3)
With myForm
.Properties("Caption") = "Choisir"
.Properties("Width") = 200
.Properties("Height") = 140
.CodeModule.InsertLines 2, "Public sub userform_initialize()" '<--| start writing your "UserForm_Initialize" sub code
With .Designer.Controls.Add("forms.listbox.1", Name:="ListBox1")
.Width = 110
.Enabled = True
.ListIndex = -1
.AddItem "Test N x M", 0
.AddItem "Test Wet strength", 1
.AddItem "Test Pressure Loss with Fine", 2
.AddItem "Test Pressure Loss with Coarse", 3
End With
.CodeModule.InsertLines 4, "End sub" '<--| finish writing your "UserForm_Initialize" sub code
Set NewButton1 = myForm.Designer.Controls.Add("Forms.commandbutton.1")
With NewButton1
.Name = "OK"
.Caption = "OK"
.Accelerator = "M"
.Top = 20
.Left = 120
.Width = 40
.Height = 20
.Font.Size = 8
.Font.Name = "Tahoma"
.BackStyle = fmBackStyleOpaque
End With
Set NewButton2 = myForm.Designer.Controls.Add("Forms.commandbutton.1")
With NewButton2
.Name = "CANCEL"
.Caption = "CANCEL"
.Accelerator = "M"
.Top = NewButton1.Top + 20
.Left = 120
.Width = 40
.Height = 20
.Font.Size = 8
.Font.Name = "Tahoma"
.BackStyle = fmBackStyleOpaque
End With
End With
myForm.CodeModule.InsertLines 20, "Private Sub ListBox1_Click()"
myForm.CodeModule.InsertLines 21, "Me.ListBox1.Show"
myForm.CodeModule.InsertLines 22, "If Me.ListBox1.ListIndex = 0 Then"
myForm.CodeModule.InsertLines 23, " Me.ListBox1.Selected(0) = True"
myForm.CodeModule.InsertLines 24, "End If"
myForm.CodeModule.InsertLines 25, "If Me.ListBox1.ListIndex = 1 Then"
myForm.CodeModule.InsertLines 26, " Me.ListBox1.Selected(1) = True"
myForm.CodeModule.InsertLines 27, "End If"
myForm.CodeModule.InsertLines 28, "If Me.ListBox1.ListIndex = 2 Then"
myForm.CodeModule.InsertLines 29, " Me.ListBox1.Selected(2) = True"
myForm.CodeModule.InsertLines 30, "End If"
myForm.CodeModule.InsertLines 31, "If Me.ListBox1.ListIndex = 3 Then"
myForm.CodeModule.InsertLines 32, " Me.ListBox1.Selected(3) = True"
myForm.CodeModule.InsertLines 33, "End If"
myForm.CodeModule.InsertLines 34, ""
myForm.CodeModule.InsertLines 35, "End Sub"
myForm.CodeModule.InsertLines 36, "Private Sub OK_Click()"
myForm.CodeModule.InsertLines 37, "If Me.ListBox1.Selected(0) = True Then"
myForm.CodeModule.InsertLines 38, " Msgbox(""Item 1 selected"" & vbLf)"
myForm.CodeModule.InsertLines 39, " Call testNxM"
myForm.CodeModule.InsertLines 40, "End If"
myForm.CodeModule.InsertLines 41, "If Me.ListBox1.Selected(1) = True Then"
myForm.CodeModule.InsertLines 42, " Msgbox(""Item 2 selected"" & vbLf)"
myForm.CodeModule.InsertLines 43, " Call testWet"
myForm.CodeModule.InsertLines 44, "End If"
myForm.CodeModule.InsertLines 45, "If Me.ListBox1.Selected(2) = True Then"
myForm.CodeModule.InsertLines 46, " Msgbox(""Item 3 selected"" & vbLf)"
myForm.CodeModule.InsertLines 47, " Call testPLFine"
myForm.CodeModule.InsertLines 48, "End If"
myForm.CodeModule.InsertLines 49, "If Me.ListBox1.Selected(3) = True Then"
myForm.CodeModule.InsertLines 50, " Msgbox(""Item 4 selected"" & vbLf)"
myForm.CodeModule.InsertLines 51, " Call testPLCoarse"
myForm.CodeModule.InsertLines 52, "End If"
myForm.CodeModule.InsertLines 53, "Unload me"
myForm.CodeModule.InsertLines 54, "End Sub"
myForm.CodeModule.InsertLines 55, "Private Sub CANCEL_Click()"
myForm.CodeModule.InsertLines 56, "Unload Me"
myForm.CodeModule.InsertLines 57, "End Sub"
VBA.UserForms.Add(myForm.Name).Show
'ThisWorkbook.VBProject.VBComponents.Remove myForm
End Sub
Wenn ich die Kontrolle und alle überprüfen möchten, ich zeigen nicht die Userform und ich kann sehe alle Elemente und kann auf sie klicken, aber wenn ich zeige, ist es nur ein Benutzerformular mit 2 Tasten und eine leere Listbox