+ Reply to Thread
Results 1 to 6 of 6

Populate ComboBox with rows that meet criterion

Hybrid View

  1. #1
    Registered User
    Join Date
    10-01-2011
    Location
    Pittsford, NY, USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Populate ComboBox with rows that meet criterion

    Hi, Everyone,
    I have a worksheet that includes 8 columns. Column 2 is the index. Column 3 has IDs.
    Given an ID, I want to know how many rows in the worksheet include the ID in Column 3.
    If more than one row, I want to load each such row into a ComboBox so I can then select the row I want to work with.
    So far, I’ve been able to count the rows that have the ID in question. But I can’t figure out how to load the rows into a ComboBox. I’d be grateful for any help!

    Private Sub cmd3_Click()
    Dim strID As String
    Dim qAuth As Integer
    Dim rng As Range, c
    
    ' [Deleted code to determine and set strID]
    
    ' Determine number of rows associated with strID
    Set rng = Range("Sheet2")
    qAuth = 0
    For c = 2 To rng.Rows.Count
        If rng.Cells(c, 3) = strID Then
        qAuth = qAuth + 1
    'While going through the rows, this might be a good time to gather data for the 
    'ComboBox, but how?
        End If
    Next c
    
    Select Case qAuth
    
    Case 0
    ' [Deleted]
    
    Case 1
    ' [Deleted]
    
    Case Else
    ' Load the ComboBox cbRows in the Form frmSelect (but how?)
    frmSelect.Show
    
    End Select
    
    End Sub
    Last edited by val64; 10-05-2011 at 12:47 PM.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Populate ComboBox with rows that meet criterion

    Hi

    I think we need to see more of your structure and how you are going to approach things. How about an example workbook.

    From your code you somehow identify an ID (this code has been deleted), and you have a combox (cbRows) on a form frmSelect. However, where do you want to load the results of the selection from the combobox? Do you really need to have a form? Maybe you could have the combobox directly on the spreadsheet, and use a dependent list approach with a dynamic named range to fill the combobox?

    From what you have described things are doable, but I'm too uncertain of your structure / approach to be able to suggest a solution.

    rylo

  3. #3
    Registered User
    Join Date
    10-01-2011
    Location
    Pittsford, NY, USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Populate ComboBox with rows that meet criterion

    Thanks, rylo. I am attaching a sample workbook, as you suggested -- and exposing you to my embarrassingly bad programming.

    The overall project is to allow clients to use Excel to keep track of their customers [worksheets("customers")] and these customers' authorizations to make appointments with us [worksheets("authorizations")]; and to send us that information every time it changes in a prescribed format (worksheets XDD and XAD).

    I don't want them messing directly with the worksheets, so I'll have the "Main" userform, with its command buttons, pop up when they open the file. They would enter data through the various userforms. At the end of the session, upon their clicking the "End" command button, the system would send the XDD and XAD sheets to me by email.

    On the "Main" userform, I have (1) Add and Authorize Customer, (2) Modify Customer, and (5) End (save and email) working smoothly. I'm now working on (3) Modify Authorization. For that, I need to choose customer (done) and then (a) find out how many authorizations, if any, the customer has (done) and, if the customer has multiple authorizations (b) find out which one needs to be modified. That's where I'm stuck.

    As I mentioned in my original post, I figured I would load a list of the authorizations in a combobox in a userform and let the user choose. But how would I load those particular rows? It seems like it should be easy to do, but I haven't been able to figure it out!

    Thanks for listening!
    VAL
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Populate ComboBox with rows that meet criterion

    Val

    Have a look at the attached. I've just dummied up something based on your format.

    Open the workbook, and then open userform1. This will fill the combobox with the list of customer IDs. Once you make a selection, a second form will open and it will contain a list of the authorizations for that customer id. Make a selection, then press the command button. This will give you a message showing the row of the item that you have selected.

    Some things to note.
    1) I've used a global variable to transfer the selected id to the second form. More convenience than anything.
    2) The combobox on the second form has 2 columns, but only 1 is visible.
    3) One column contains the authorization, and the other one contains the row number of the item.
    4) The message box shows how you can access the selected item row number.

    Once you know the row number of the item that you want to action, then it should be easy to select it, modify it etc.

    HTH

    rylo
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    10-01-2011
    Location
    Pittsford, NY, USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Populate ComboBox with rows that meet criterion

    Amazing! Thank you so very much for taking the time to do this. It looks very elegant. I'll let you know how it works out when I plug it into my project.

    V.

  6. #6
    Registered User
    Join Date
    10-01-2011
    Location
    Pittsford, NY, USA
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: Populate ComboBox with rows that meet criterion

    The code you so kindly provided worked perfectly. Thank you again.

    Here's what I ended up doing with it. (I wanted the combobox to have multiple columns, as the index number would not mean much to the user; and I wanted to filter the rows further (to exclude rows marked "D" for delete). I am posting it with my annotations for the benefit of others.

    Dim rng As Range
    'set lCriterion
    
    With Sheets("Sheet1")
        Set rng = .Range("C2:C" & .Cells(Rows.Count, 3).End(xlUp).Row)
    'where C and 3 represent the index column (we'll compare values in this 
    ' column to lCriterion)
    'if you were to choose multiple columns, the first column would serve as
    ' index (so don't bother)
      End With
    
    For Each ce In rng
    'where ce is an undeclared variable
        If ce.Value = lCriterion Then
    'where lCriterion is your criterion
          If ce.Offset(0, -2) <> "D" Then
    'and here's an additional condition: exclude rows that have "D" in column 1 (i.e.,
    ' column C minus 2)
          ComboBox1.AddItem ce.Offset(0, -1).Value
    'Adds the value in column B (i.e., C minus 1) to the ComboBox1
          ComboBox1.List(ComboBox1.ListCount - 1, 1) = ce.Offset(0, 0)
    'Adds the value in column C (ce itself) to the same line of
    ' ComboBox1 (ListCount -1, x) in the next column (ListCount x , 1)
          ComboBox1.List(ComboBox1.ListCount - 1, 2) = ce.Offset(0, 1)
    'Adds the value in column D (ce offset by 1) to the same line of
    ' ComboBox1 (ListCount -1, x) in the next column (ListCount x , 2)
          ComboBox1.List(ComboBox1.ListCount - 1, 3) = ce.Offset(0, 2)
    'And so on so that the user can see the information in 7 columns before
    ' choosing the row
          ComboBox1.List(ComboBox1.ListCount - 1, 4) = ce.Offset(0, 3)
          ComboBox1.List(ComboBox1.ListCount - 1, 5) = ce.Offset(0, 4)
          ComboBox1.List(ComboBox1.ListCount - 1, 6) = ce.Offset(0, 5)
    '      ComboBox1.List(ComboBox1.ListCount - 1, 7) = ce.Row
    'Include the line above if you want ComboBox1 to include the row number
    ' in Sheet1 that the line represents
          End If
        End If
      Next ce

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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