PDA

View Full Version : [SOLVED] Userform ListBox



Stromma
04-06-2005, 06:25 AM
Hi

I know how to add all sheets in a workbook to a listbox and then activate them by selecting them in the listbox. Now I'm trying to do something similar with userforms.

I manage to add all workbook userform names in the listbox, but when i select a form to load I get an error. If i use an msg to show the selected value I've get the correct userform name, but when I try to actually load a form it won't work.

I tried using the selected userform name as well as the userform number in the click event.

Any idea?

Stromma

Steiner
04-06-2005, 07:03 AM
Since you can only access loaded forms, you'll have to load the selected form first. Make sure you have a listbox (ListBox1) with the userform names and a button (CommandButton1), then try the following code:


Private Sub CommandButton1_Click()
Dim i%
UserForms.Add ListBox1.Text
For i = 0 To UserForms.Count - 1
If UserForms(i).Name = ListBox1.Text Then
UserForms(i).Show
Exit For
End If
Next i
End Sub

This code first loads the form by adding it to the Userforms collection.
Since you can only access the form by it's index in that collection, we have to go through the whole collection and compare the name to show the right one.

Daniel

Stromma
04-06-2005, 06:05 PM
Worked like a charm!

Thanks!

Stromma