+ Reply to Thread
Results 1 to 14 of 14

userform combobox fills from selection on sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-16-2010
    Location
    glasgow scothland
    MS-Off Ver
    Excel 2007
    Posts
    112

    userform combobox fills from selection on sheet

    Hello all,

    I am stuck trying to fill comboboxs on a user form from what a person selects on the sheet.

    In attached if person picks 5 fruits from the list each combobox will load the 5 choices.
    The fruits are listed in column B while the person will put an X in column C.
    The fruits in B with the X in column C will load in combo box.

    This sounds easy but not for me.

    Please take a look at attached.
    Any help is appreciated.
    Attached Files Attached Files
    Last edited by mdshotgun; 11-27-2011 at 07:22 PM.

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: userform combobox fills from selection on sheet

    Hi mdshotgun

    Remove the code from open workbook event
    and in the userform add

    Option Explicit
    Private Sub ComboBox1_Enter()
        Dim rng As Range
        Dim xFruit
        Me.ComboBox1.Clear
        Set rng = Range("Fruits")
        For Each xFruit In rng
            Debug.Print xFruit
            If UCase(xFruit.Offset(0, 1).Value) = "X" Then
                Me.ComboBox1.AddItem xFruit
            End If
        Next
    End Sub
    Private Sub ComboBox2_Enter()
        Dim rng As Range
        Dim xFruit
        Me.ComboBox2.Clear
        Set rng = Range("Fruits")
        For Each xFruit In rng
            Debug.Print xFruit
            If UCase(xFruit.Offset(0, 1).Value) = "X" Then
                Me.ComboBox2.AddItem xFruit
            End If
        Next
    End Sub
    and change the showmodlal setting of the userform to false
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: userform combobox fills from selection on sheet

    Hi,

    Put this in the userforms code

    Option Explicit
    Sub Userform_Initialize()
    
    Dim arr() As String
    Dim counter As Integer
    Dim i As Integer
    
    counter = 0
    For i = 4 To 13
        If Sheets(1).Cells(i, 3) = "x" Then
            ReDim Preserve arr(counter)
            arr(counter) = Sheets(1).Cells(i, 2)
            counter = counter + 1
        End If
    Next i
    
    With ComboBox1
        .Clear
        .List = arr
        .ListIndex = 0
    End With
    
    With ComboBox2
        .Clear
        .List = arr
        .ListIndex = 0
    End With
    
    With ComboBox3
        .Clear
        .List = arr
        .ListIndex = 0
    End With
    
    With ComboBox4
        .Clear
        .List = arr
        .ListIndex = 0
    End With
    
    With ComboBox5
        .Clear
        .List = arr
        .ListIndex = 0
    End With
    
    End Sub
    When the userform loads, it will take the values marked with x and put them in each combobox
    Please take time to read the forum rules

  4. #4
    Forum Contributor
    Join Date
    05-16-2010
    Location
    glasgow scothland
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: userform combobox fills from selection on sheet

    wow that is some excellent code from you both.

    Thank you very much.

    Before i mark this as solved.
    Could i ask i last question.
    When the 5 fruits appear in the comboboxes when the person picks apples
    in combo1 could it be done so example apple will now disappear from selection for
    the other comboboxes.
    so when they get to combo5 there will be only i choice left.

    Thanks again for the help.

  5. #5
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: userform combobox fills from selection on sheet

    Yes,

    Put this in the userform code

    Sub ComboBox1_Click
    
    With Combobox2
        .RemoveItem Combobox1.value
    end with
    
    With Combobox3
        .RemoveItem Combobox1.value
    end with
    
    With Combobox4
        .RemoveItem Combobox1.value
    end with
    
    With Combobox5
        .RemoveItem Combobox1.value
    end with
    
    End sub
    Create similar codes for cb2-5

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: userform combobox fills from selection on sheet

    or:
    Sub Userform_Initialize()
      for j=1 to 5
       me("Combobox" & j).list=sheets(1).range("B4:B13").value
      next
    End Sub



  7. #7
    Forum Contributor
    Join Date
    05-16-2010
    Location
    glasgow scothland
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: userform combobox fills from selection on sheet

    Thanks again for the code and help.

    @snb for some reason your code does'nt work.It just fills the comboboxes with all the fruits.

    @steffen your code is giving an error.

    Thanks anyway for trying.

  8. #8
    Valued Forum Contributor Steffen Thomsen's Avatar
    Join Date
    10-15-2010
    Location
    Kolding, Denmark
    MS-Off Ver
    Excel 2007 and Excel 2010
    Posts
    953

    Re: userform combobox fills from selection on sheet

    On which line is it giving an error?

    *EDIT

    By the way, if it's only possible to select a different value in each combobox, why not populate each combobox with a different value, and skip the user selection?
    Last edited by Steffen Thomsen; 11-27-2011 at 07:02 AM.

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: userform combobox fills from selection on sheet

    You could have adapted the code yourself

    Sub Userform_Initialize()
      for j=1 to 5
       me("Combobox" & j).list=filter([transpose(if(C4:C13="X","~",B4:B13))],"~",false)
      next
    End Sub

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: userform combobox fills from selection on sheet

    Try this
    Option Explicit
    Dim iX As Integer
    
    Private Sub ComboBox1_Change()
        RemoveIt (2)
    End Sub
    
    
    Private Sub ComboBox2_Change()
        RemoveIt (3)
    End Sub
    
    Private Sub ComboBox3_Change()
        RemoveIt (4)
    End Sub
    
    Private Sub ComboBox4_Change()
        RemoveIt (5)
    End Sub
    
    Private Sub UserForm_Initialize()
        Dim rCl As Range
        For iX = 1 To 5
            For Each rCl In Range("Fruits")
                Me("Combobox" & iX).AddItem rCl.Text
            Next rCl
        Next iX
    End Sub
    
    Sub RemoveIt(iJ As Integer)
        For iX = iJ To 5
            Me("Combobox" & iX).RemoveItem Me("ComboBox" & iJ - 1).ListIndex
        Next iX
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  11. #11
    Forum Contributor
    Join Date
    05-16-2010
    Location
    glasgow scothland
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: userform combobox fills from selection on sheet

    Hello all,

    I would like to thank you all for the replies and help.
    It took me a while to figure out how to use the codes been a learner,
    but eventually i did.

    So thank you all again.

  12. #12
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: userform combobox fills from selection on sheet

    Thanks for the rep. whicc code did you use?
    Last edited by royUK; 11-28-2011 at 02:09 AM.

  13. #13
    Forum Contributor
    Join Date
    05-16-2010
    Location
    glasgow scothland
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: userform combobox fills from selection on sheet

    Roy,

    I can use either pikes or steffens for first part to fill combo's.

    i am using your code for the second part.

    The codes together are not working exactly as i wanted but i will spend sometime trying to figure it out.

  14. #14
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: userform combobox fills from selection on sheet

    I know you cannot use RemovItem if the Combo is populated using RowSource, I'm not sure about List Ptroprty. Hence, I used a Loop to load each combo using AddItem

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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