+ Reply to Thread
Results 1 to 10 of 10

Creating a User Form with variable number of Controls buttons

Hybrid View

  1. #1
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,480
    I prefer the listbox approach to this problem where number of entries is variable.

    Add a listbox to the userform with this code added to the Initialize event.

    
    ListBox1.ListStyle = fmListStyleOption
    ListBox1.MultiSelect = fmMultiSelectMulti
    For i = 1 To 10
    If Cells(i, 1) <> "" Then
        ListBox1.AddItem Cells(i, 1).Value
    End If
    Next
    Cheers
    Andy
    www.andypope.info

  2. #2
    Registered User
    Join Date
    08-30-2005
    Posts
    67
    Thanks Andy. That's a good solution. I was not sure the list box will give me the multiple selection choise. Seems like it's all possible.

    Still, I'm trying to figure out where do I have to put the code to sort the data prior to the form being initialized.

    Thanks beforehand for any advise.

    Eduard

  3. #3
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,480
    Either in the initalize event, before the code to deal with checkboxes/listbox.
    Or in the Click event of the button

    Private Sub CommandButton1_Click()
    
    ' code to do thing with cells
    ' ....
    
    ' now show userform
    UserForm1.Show
    
    End Sub

  4. #4
    Registered User
    Join Date
    04-24-2008
    Location
    Zurich
    Posts
    45
    Try this:
    Private Sub UserForm_Initialize()
    
    Dim i As Long
    
    
    Range("A1:A10").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    
    
    For i = 1 To 10
    If Cells(i, 1) = "" Then
        UserForm1.Controls.Item(i - 1).Visible = False
    Else
        UserForm1.Controls.Item(i - 1).Visible = True
        UserForm1.Controls.Item(i - 1).Caption = Cells(i, 1).Value
    End If
    Next
    End Sub

  5. #5
    Registered User
    Join Date
    08-30-2005
    Posts
    67
    Thanks, Guys. It works either way for me.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VLOOKUP Funtion within a User Form
    By bern in forum Excel General
    Replies: 4
    Last Post: 01-06-2016, 06:11 AM
  2. User form problems
    By lapot in forum Excel General
    Replies: 4
    Last Post: 12-15-2006, 02:02 PM
  3. replacing values in a variable number of columns
    By mwc0914 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-07-2006, 10:42 PM
  4. possible to delay comments until user form receives input?
    By noisepoet in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-28-2006, 01:47 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1