Home » excel » excel – Using instr to find 3 matching cell values from a source table row appearing in another table row

excel – Using instr to find 3 matching cell values from a source table row appearing in another table row

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am very new to coding with it, and what appears below probably looks quite horrible.

What this code currently does is do an instr search for all 3 required values appearing in a single, and absolutely defined (for test purposes), row of another table, in a separate worksheet. Copies the A cell value from that row, pastes it into the cell next to the source table row, currently being searched, and colour codes it with a green fill.

What I want it to do, is be aware that there is a whole other table of data in the other worksheet, and have it search row by row for all 3 required values matching in a given row.

Once it gets an exact hit, I want it to output the A cell value for the row that has been confirmed to be a match of all 3 required values.

The table in the other sheet is dynamic, in that it increases or decreases in total number of rows day be day.

Is anyone kind enough to be able to help me with this?

Now, here is my novice mishmash of code:

Private Sub Match_Click()
Dim i As Integer, row As Integer, narrative1 As String, transDate As Date, 
amount As Double, result As String
row = 2
i = 1
narrative1 = Worksheets("Sheet2").Range("D" & row)
transDate = Worksheets("Sheet2").Range("B" & row)
amount = Worksheets("Sheet2").Range("J" & row)


Do While Cells(i, 1).Value <> ""

If narrative1 > "" Then
  If InStr(1, UCase(Worksheets("Sheet1").Range("D22")), UCase(narrative1)) And 
  InStr(1, Worksheets("Sheet1").Range("B22"), transDate) And InStr(1, 
  Worksheets("Sheet1").Range("H22"), amount) Then
  result = Worksheets("Sheet1").Range("A3").Value
Else
  result = ""
End If
End If

i = i + 1

If Worksheets("Sheet2").Range("A" & row).Value = "" Then result = ""
Worksheets("Sheet2").Range("K" & row).Value = result

If result <> "" Then Worksheets("Sheet2").Range("K" & row).Interior.Color = 
RGB(198, 224, 180)

If Worksheets("Sheet2").Range("A" & row).Value = "" Then 
Worksheets("Sheet2").Range("K" & row).Interior.ColorIndex = xlNone

row = row + 1
narrative1 = Worksheets("Sheet2").Range("D" & row)
transDate = Worksheets("Sheet2").Range("B" & row)
amount = Worksheets("Sheet2").Range("J" & row)
Loop

End Sub
How to&Answers:

I think the following code will do what you expect, I’ve commented it to let you know what its doing (I haven’t tested it, but I’m pretty sure it will do the job):

Private Sub Match_Click()
Dim i As Long 'These should be Long instead of Integer, as Excel has more cells than Integer has values.
Dim row As Long
Dim LastRow As Long
Dim LastRow2 As Long
Dim narrative1 As String
Dim transDate As String
Dim amount As Double
Dim result As String
Dim val1 As Integer
Dim val2 As Integer
Dim val3 As Integer

LastRow2 = Worksheets("Sheet2").Cells(Worksheets("Sheet2").Rows.Count, "A").End(xlUp).row 'get the lastrow of Sheet2
LastRow = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, "A").End(xlUp).row 'get the lastrow of Sheet1

For i = 1 To LastRow2 'loop from row 1 to last on Sheet2
narrative1 = Worksheets("Sheet2").Range("D" & i) 'get the variables to compare
transDate = Worksheets("Sheet2").Range("B" & i)
amount = Worksheets("Sheet2").Range("J" & i)
    For x = 1 To LastRow ' loop through row 1 to last on Sheet1
        If narrative1 <> "" Then
            val1 = InStr(Worksheets("Sheet1").Cells(x, 4).Value, narrative1) 'number 4 represents column D
            val2 = InStr(Worksheets("Sheet1").Cells(x, 2).Value, transDate) 'number 2 represents column B
            val3 = InStr(Worksheets("Sheet1").Cells(x, 8).Value, amount) 'number 8 represents column H

            If val1 > 0 And val2 > 0 And val3 > 0 Then 'if all three have been found
                result = Worksheets("Sheet1").Cells(x, 1).Value 'get result
                Worksheets("Sheet2").Range("K" & LastRow2 + 1).Value = result 'paster result into next free row on Sheet2 column K
                If Worksheets("Sheet2").Cells(x, 1).Value <> "" Then Worksheets("Sheet2").Range("K" & LastRow2 + 1).Interior.ColorIndex = 4
            Else
                result = ""
            End If
        End If
     Next x
Next i
End Sub