+ Reply to Thread
Results 1 to 28 of 28

ComboBox and .Find

Hybrid View

  1. #1
    Registered User
    Join Date
    05-08-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    80

    ComboBox and .Find

    Warning: code will be infinite loop!

    Hello, I have multiple comboboxes in a userform and I want the second combobox list to be dependent upon the first combobox. The first combobox simply goes through a spreadsheet and adds the cell values from the first column. I want the second combobox to add items that are next to the cell that matches the value of the selected item from the first combobox. I currently have
    Private Sub ComboBox1_Change()
    
    With Sheets("Schedule")
        idx = ComboBox1.ListIndex
        Set x = .Range("A:A").Find(what:=ComboBox2.Value, LookIn:=xlValues, lookat:=xlPart)
        If Not x Is Nothing Then
            firstAddress = x.Address
            Do
                ComboBox2.AddItem .Cells(x.Row, "B").Value
                
                Set x = .Range("A:A").FindNext(x)
            Loop While Not x Is Nothing And x.Address <> firstAddress
        End If
    End With
    
    End Sub
    but this is not working. It is difficut to explain what I would like exactly, so I will upload the workbook as well, if needed.
    Thanks in advance!

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ComboBox and .Find

    Why not just loop through the range that has the values you want in Combobox2?

    If a value has the selected value from Combobox1 adjacent to it then add it to Combobox2.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    05-08-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: ComboBox and .Find

    Wait, I am not sure if that would work... Could you elaborate?

  4. #4
    Registered User
    Join Date
    05-08-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: ComboBox and .Find

    Good Thinking. I also realized a simple, silly error in my code that fixes it anyways. Your idea is much simpler though.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ComboBox and .Find

    I'd need to know the columns/sheet(s) involved to post something specific.

    Can you attach a sample workbook?

  6. #6
    Registered User
    Join Date
    05-08-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: ComboBox and .Find

    Sure thing. It is on the OutgoingUF. And it uses the 'Schedule' WS.

    Correct Document.xlsm

  7. #7
    Registered User
    Join Date
    05-08-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: ComboBox and .Find

    The way I have it now works correctly, but there are duplicate 'Customers' showing up in ComboBox1, which is rather annoying. Although I understand that this problem would have to be addressed in a new thread.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ComboBox and .Find

    Which form should I be looking at?

    I think it's IncomeUF but I'm not sure.

  9. #9
    Registered User
    Join Date
    05-08-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: ComboBox and .Find

    It is on the OutgoingUF. And it uses the 'Schedule' WS.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ComboBox and .Find

    Try this.
    Private Sub UserForm_Initialize()
    Dim dicCustomers As Object
    
        Set dicCustomers = CreateObject("Scripting.Dictionary")
    
        With Worksheets("Schedule")
            x = Cells(Rows.Count, 1).End(xlUp).Row
            For Row = 2 To x
    
                If Not dicCustomers.exists(.Cells(Row, 1).Value) Then
                    dicCustomers.Add .Cells(Row, 1).Value, Row
                End If
    
            Next Row
        End With
    
        ComboBox1.List = dicCustomers.keys
    
        With TextBox3
            .Text = "mm/dd/yy"
        End With
    
    End Sub
    
    
    Private Sub ComboBox1_Change()
    Dim rng As Range
    Dim cl As Range
    
        If ComboBox1.ListIndex <> -1 Then
    
            ComboBox2.Clear
    
            With Worksheets("Schedule")
                Set rng = .Range("A2", .Cells(.Rows.Count, 1).End(xlUp))
            End With
    
            For Each cl In rng
                If cl.Value = ComboBox1.Value Then
                    ComboBox2.AddItem cl.Offset(, 1).Value
                End If
            Next cl
        End If
    End Sub

  11. #11
    Registered User
    Join Date
    05-08-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: ComboBox and .Find

    What is the advantage of this code vs. what I have done? Is it more efficient?
    Thank you very much for putting time into looking at it though, I greatly appreciate it.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ComboBox and .Find

    It doesn't list the customers multiple times in the first combobox.

  13. #13
    Registered User
    Join Date
    05-08-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: ComboBox and .Find

    It doesn't seem to be working when I run it...

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ComboBox and .Find

    It works when I run it.

    How exactly did you use it? It should replace the existing code for UserForm Initialize and Combobox1_Change.

  15. #15
    Registered User
    Join Date
    05-08-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: ComboBox and .Find

    Yes, that is what I did. That's odd. Is there any other way to remove duplicates from the combobox? Other than that, my code works fine.

  16. #16
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ComboBox and .Find

    The code I posted prevents duplicates in combobox1, see the attached file.
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    05-08-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: ComboBox and .Find

    I have just downloaded your version and the first and second comboboxes are not loading. This is very peculiar.

  18. #18
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ComboBox and .Find

    Which userform are you looking at?

    The code was for OutgoingUF, as you indicated here that was the one of interest.
    Quote Originally Posted by an earlier post
    It is on the OutgoingUF. And it uses the 'Schedule' WS.

  19. #19
    Registered User
    Join Date
    05-08-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: ComboBox and .Find

    Capture.jpg
    Yes, as you can see here, when I click the combobox, nothing shows up

  20. #20
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ComboBox and .Find

    In the original code there's a missing dot before Cells in this line of code.
     x = Cells(Rows.Count, 1).End(xlUp).Row
    I should have picked it up but missed it.

    So if you add that missing dot in the code I posted the code will work.

  21. #21
    Registered User
    Join Date
    05-08-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: ComboBox and .Find

    That worked much better. Thank you. For some reason I thought I would simply be able to copy the subprocedure and change the names around, but that's not working. Any chance you know what I did incorrectly? This is to fill up the third ComboBox with the PO Number, by the way.
    Private Sub ComboBox2_Change()
    Dim rng As Range
    Dim cl As Range
    
        If ComboBox2.ListIndex <> -1 Then
    
            ComboBox3.Clear
    
            With Worksheets("Schedule")
                Set rng = .Range("A4", .Cells(.Rows.Count, 1).End(xlUp))
            End With
    
            For Each cl In rng
                If cl.Value = ComboBox2.Value Then
                    ComboBox3.AddItem cl.Offset(, 1).Value
                End If
            Next cl
        End If
    End Sub

  22. #22
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ComboBox and .Find

    Which range should you be loopng through?

    This is setting it as column A, the customer column, starting at row 4.
    With Worksheets("Schedule")
                Set rng = .Range("A4", .Cells(.Rows.Count, 1).End(xlUp))
            End With
    Shouldn't you be looping through the Part# column, starting at row 2?

  23. #23
    Registered User
    Join Date
    05-08-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: ComboBox and .Find

    Yes you are correct. So now I have this, but it is still not working... it has been a long day
    Private Sub ComboBox2_Change()
    Dim rng As Range
    Dim cl As Range
    
        If ComboBox2.ListIndex <> -1 Then
    
            ComboBox3.Clear
    
            With Worksheets("Schedule")
                Set rng = .Range("B2", .Cells(.Rows.Count, 1).End(xlUp))
            End With
            
            For Each cl In rng
                If cl.Value = ComboBox2.Value Then
                    ComboBox3.AddItem cl.Offset(, 1).Value
                End If
            Next cl
        End If
    End Sub

  24. #24
    Registered User
    Join Date
    05-08-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: ComboBox and .Find

    So I changed it to this:
    ComboBox3.AddItem cl.Offset(, 2).Value
    but it only works some of the time. For instance, it works for the ADMO selection in combobox1, but not Stanley II.
    Is this something to do with the
    If ComboBox2.ListIndex <> -1 Then
    statement?

  25. #25
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ComboBox and .Find

    Have a look at the address of rng.

    You can do that by putting a breakpoint on the End With, running the form, selecting from Combobox1 and then Combobox2.

    The code will then halt and you can find the address by going to the Immediate Window (CTRL+G) and entering this.
    ?rng.Address

  26. #26
    Registered User
    Join Date
    05-08-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: ComboBox and .Find

    It has given me this:
    $A$2:$B$13

  27. #27
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: ComboBox and .Find

    Which I don't think is quite right.

    In this code,
    With Worksheets("Schedule")
                Set rng = .Range("B2", .Cells(.Rows.Count, 1).End(xlUp))
    End With
    try changing .Cells(Rows.Count,1) to .Cells(Rows.Count,2), or to make it clearer .Cells(.Rows.Count, "B").

    By the way, there's another problem - some of the part numbers on the worksheet are numeric but all of the part numbers in the worksheet are text.

    That won't cause an error but it will prevent the code finding matches.

    To deal with that change this,
           If cl.Value = ComboBox2.Value Then
    to this.
           If cl.Text = ComboBox2.Value Then

  28. #28
    Registered User
    Join Date
    05-08-2013
    Location
    Ann Arbor, MI
    MS-Off Ver
    Excel 2010
    Posts
    80

    Re: ComboBox and .Find

    YES! Thank you so much!

+ 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