Consulting

Results 1 to 8 of 8

Thread: Find row number and pass it to a variable

  1. #1
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location

    Find row number and pass it to a variable

    I have a userform with a comboBox.
    On the worksheet the data Range for the comboBox is ( B5:B15)

    How do I match the value selected in the combobox on the UserForm to the row number in Range ( B5:B15) and pass the row number to a variable nNum.

    How do you use Index/Match in VBA code?

    Thanks

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You don't say what you want to do with the row info but this method which also populates the combobox for you will return values from the selected row using find......see attached.

     
    Private Sub UserForm_Initialize()
    With Sheets("Sheet1")
        ComboBox1.List = Range(.Cells(1, 1), .Cells(1, 1).End(xlDown)).Value
    End With
    End Sub
    
    Private Sub ComboBox1_Change()
    Dim rng As Range
    With Sheets("Sheet1")
        Set rng = Range(.Cells(1, 1), .Cells(1, 1).End(xlDown))
    End With
    With rng.Find(ComboBox1, lookat:=xlWhole)
        Label1.Caption = .Offset(, 1)
        Label2.Caption = .Offset(, 2)
    End With
    End Sub
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    Thanks Lucas:

    Will check it out in a couple of hours when I return.

  4. #4
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    Lucas:

    I did a quick look at the code.
    I need a rownumber to be the result of this bit:
     
        Set rng = Range(.Cells(1, 1), .Cells(1, 1).End(xlDown)) 
    End With 
    With rng.Find(ComboBox1, lookat:=xlWhole)
    rng. value or something along those lines to know which row it found the match

    Thanks

    Edit Lucas: VBA tags added to code

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Like this:
    Private Sub ComboBox1_Change()
    Dim rng As Range
    With Sheets("Sheet1")
        Set rng = Range(.Cells(1, 1), .Cells(1, 1).End(xlDown))
    End With
    With rng.Find(ComboBox1, lookat:=xlWhole)
        Label1.Caption = .Row
        MsgBox .Row
    End With
    End Sub
    or
     
    Label1.Caption = "The item was found on row " & .Row
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    384
    Location
    That works great. Thanks

    I can set rsRow= .Row
    and use it as a variable.

    Where is the details on how to attach a sheet &
    How do you set this post as solved ?

  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    To attach your workbook click on post reply at the bottom left of the last post, post your message and then scroll down and look for a button that says "manage attachments"

    Mark your thread solved using the thread tools at the top of the page.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    ps when posting code, select the code and hit the vba button to format it for the forum.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

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