Consulting

Results 1 to 3 of 3

Thread: Userform ListBox

  1. #1
    VBAX Regular
    Joined
    May 2004
    Location
    Sweden
    Posts
    21
    Location

    Userform ListBox

    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

  2. #2
    VBAX Tutor
    Joined
    May 2004
    Location
    Germany, Dresden
    Posts
    217
    Location
    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

  3. #3
    VBAX Regular
    Joined
    May 2004
    Location
    Sweden
    Posts
    21
    Location
    Worked like a charm!

    Thanks!

    Stromma

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •