+ Reply to Thread
Results 1 to 10 of 10

Creating a User Form with variable number of Controls buttons

Hybrid View

  1. #1
    Registered User
    Join Date
    04-24-2008
    Location
    Zurich
    Posts
    45
    You may create a userform with lets say 10 checkboxes and assign the code below to the userform. You have to verify the item indices of the checkboxes (under properties listed as TabIndex). The code as it is assumes index 0 to 9 for the 10 checkboxes.

    Private Sub UserForm_Initialize()
    
    Dim i As Long
    
    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
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    08-30-2005
    Posts
    67
    Thanks Kuskush! This code might do for a while.

    Would there be a more practical solution? Cause I don't want to create a VBA User Form with 100 checkboxes just to make sure that all the records are picked up. Besides that the size of such a User Form will be not very "user-friendly". And the boxes will be dispersed all other the form.

  3. #3
    Registered User
    Join Date
    08-30-2005
    Posts
    67
    Dear Kuskush,

    I have one more thing to ask, if you don't mind.

    Would it be possible to run a User Form after or within the a macro?

    What I'm trying to get, if to take your example, is to sort the cells A1:A10 first and after that the User Form to pop up.

    Thanks,
    Eduard
    Attached Files Attached Files

  4. #4
    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

  5. #5
    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

  6. #6
    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

  7. #7
    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

+ 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