Reputation: 3
I Have a code for a userform with a listbox but when i show the userform the Listbox is empty but when i don't Show i can see the items.
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 create()
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
When i want to verify the control and all, i don't show the userform and i can see all the items and can click on them but when i show, it's just a userform with 2 buttons and an empty listbox
Upvotes: 0
Views: 117
Reputation: 412
Well I can't answer exactly why yours doesn't work but I guess it is something to do with the transition to run time. However a simple solution is to add to this list during run time IE add
MyForm.codemodule.insertlines 58, "Private Sub UserForm_Activate()"
MyForm.codemodule.insertlines 59, "with me.listbox1"
MyForm.codemodule.insertlines 60, " .additem ""blah blah"""
MyForm.codemodule.insertlines 61, "end with"
MyForm.codemodule.insertlines 62, "End Sub"
to the bottom of your code and it should work...
Upvotes: 0