+ Reply to Thread
Results 1 to 3 of 3

Using range.find to find two specific values in two columns

Hybrid View

  1. #1
    Registered User
    Join Date
    10-17-2012
    Location
    usa
    MS-Off Ver
    Excel 2003
    Posts
    2

    Using range.find to find two specific values in two columns

    Hello,

    I have been trying to get the code below to work for a while now and am pretty much stuck.

    What I have made is a userform that the user inputs a job # and seq # (columns A and B), and it will search for those two user values and print out columns A B C D into the userform BOX.

    I have gotten this to work by only using one of the values (JOB) , however I am stumped as to how to search another column (column B) to get the row that has that JOB AND SEQ.

    For example, here is how the table will look:

    JOB SEQ MACHINE 1 MACHINE 2
    100 202 MAT MAT
    100 204 MAT MAT

    I want the user to type in

    JOB : 100
    SEQ 202

    and see in the box

    100 202 MAT MAT


    Here is what I have so far:

    Private Sub cmdFind_Click()
    Dim searchText As Variant, FirstAddr As String, searchText1 As String
    Dim FoundCell As Range, LastCell As Range, searchRange As Range, searchrange1 As Range
    Dim i As Integer, endRow As Long
    Dim foundTarget As Boolean
    
        searchText = Array(JOBS.Text, SEQ.Text)
    
        'If Len(searchText) = 0 Then Exit Sub
       
        Application.ScreenUpdating = False
        Worksheets("jobs").Select
        Range("A1:B1000").End(xlDown).Select
        endRow = ActiveCell.Row
        Range("A1:B1000").Select
        Application.ScreenUpdating = True
        
        Worksheets("jobs").Select
        Set searchRange = Range("A2:B1000")
        
        Me.lstCustSearch.Clear
        foundTarget = True
        
        With searchRange
            Set LastCell = .Cells(.Cells.Count)
        End With
    
        Set FoundCell = searchRange.Find(what:=searchText, after:=LastCell)
    
        If Not FoundCell Is Nothing Then
            FirstAddr = FoundCell.Address
        Else
            foundTarget = False
        End If
    
        i = 0
        Do Until FoundCell Is Nothing
    
            Me.lstCustSearch.AddItem Cells(FoundCell.Row, 1).Value
            Me.lstCustSearch.List(i, 1) = Cells(FoundCell.Row, 2).Value
            Me.lstCustSearch.List(i, 2) = Cells(FoundCell.Row, 3).Value
            Me.lstCustSearch.List(i, 3) = Cells(FoundCell.Row, 4).Value
            Set FoundCell = searchRange.FindNext(after:=FoundCell)
            If FoundCell.Address = FirstAddr Then
                Exit Do
            End If
            i = i + 1
            
        Loop
        
        If Not foundTarget Then
            MsgBox "No data found for " & searchText
        Else
            Me.JOBS.Text = ""
        End If
        
        Me.JOBS.SetFocus
        
    End Sub

    Thanks for anyones help

  2. #2
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,551

    Re: Using range.find to find two specific values in two columns

    Maybe this, it would be easier to help if we could see your workbook.

        Set searchRange = Worksheets("jobs").Range("A:A")
        
        Me.lstCustSearch.Clear
        
        With searchRange
            Set LastCell = .Cells(.Cells.Count)
        End With
    
        Set FoundCell = searchRange.Find(what:=JOBS.Text, after:=LastCell)
        If Not FoundCell Is Nothing Then
            FirstAddr = FoundCell.Address
        End If
        Do Until FoundCell Is Nothing
        'Check if SEQ in column b matches SEQ.Text if it does add values to form
        ' and exit do. If not keep checking
            If FoundCell.Offset(0, 1) = SEQ.Text Then
                Me.lstCustSearch.AddItem Cells(FoundCell.Row, 1).Value
                Me.lstCustSearch.List(i, 1) = Cells(FoundCell.Row, 2).Value
                Me.lstCustSearch.List(i, 2) = Cells(FoundCell.Row, 3).Value
                Me.lstCustSearch.List(i, 3) = Cells(FoundCell.Row, 4).Value
              Exit Do
            End If
            Set FoundCell = searchRange.FindNext(after:=FoundCell)
            If FoundCell.Address = FirstAddr Then
                Exit Do
            End If
        Loop
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  3. #3
    Registered User
    Join Date
    10-17-2012
    Location
    usa
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Using range.find to find two specific values in two columns

    Mike,

    Thank you VERY MUCH! That is exactly what I wanted to do and it works very nicely.

    Thanks for the quick reply as well!

    -Mike

+ 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