View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default additem to listbox built on the fly

Sub try1()

Dim TempForm
Dim NewListBox As MSForms.ListBox

Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_M SForm)
TempForm.Properties("width") = 150

Set NewListBox = TempForm.Designer.Controls.Add("forms.listbox.1",
"Listbox1")

With NewListBox
.Height = 80
.Left = 20
.Top = 30

End With
With TempForm.CodeModule
X = .CountOfLines
.InsertLines X + 1, "Sub Userform_Initialize()"
.InsertLines X + 2, " Listbox1.Additem ""Trevor"""
.InsertLines X + 3, " ListBox1.Additem ""Fred"""
.InsertLines X + 4, "End Sub"
End With

VBA.UserForms.Add(TempForm.Name).Show

End Sub

Additem happens at runtime - so your form needs to exist to additem - i.e.
additem is a method, not a property.

Regards,
Tom Ogilvy


"trevor" wrote in message
...
Hi,

I'm trying to construct a form on the fly with a listbox.
I can make the form and listbox appear but can't get the
additem method to populate the listbox. The code I'm
playing with follows. What am I missing? Thanks for any
thoughts.

Trevor

Sub try1()

Dim TempForm
Dim NewListBox As MSForms.ListBox

Set TempForm = ThisWorkbook.VBProject.VBComponents.Add
(vbext_ct_MSForm)
TempForm.Properties("width") = 150

Set NewListBox = TempForm.Designer.Controls.Add
("forms.listbox.1")

With NewListBox
.Height = 80
.Left = 20
.Top = 30
.AddItem "Trevor"
.AddItem "Fred"
End With

VBA.UserForms.Add(TempForm.Name).Show

End Sub