Home » excel » (VBA) Excel : action if content in two columns is matching

(VBA) Excel : action if content in two columns is matching

Posted by: admin May 14, 2020 Leave a comment

Questions:

A picture is worth a thousand words so to make it clear, here is a picture to illustrate the situation:
enter image description here

So I have in a column C some contents (string and integer) from C2 to C16.
I have another column G some city names (string) from G21 to G25.

Here is what I try to do : I want my method probaCity() to look for matches between these two columns. If there is a match, I want it to display some value (10 for example) in the corresponding row of the G column (in the array “Probablity of being the city”), if no match, then it’s gonna be 0.

So, here in the cell G13 it should write 10 as there is a match for “Quezon” and 0 in all the other cells of the array.

Here is my code :

Sub probaCity()
Dim count As Integer, myResult As Boolean

For count = 2 To lastRow
Range("C" & count).Activate
myResult = IsNumeric(Application.Match(ActiveCell.Value, Range("G21:G25"), 0))

If myResult = True Then
Range("G" & ActiveCell.Row).Value = Range("G" & ActiveCell.Row).Value + 0
Else
Range("G" & ActiveCell.Row).Value = Range("G" & ActiveCell.Row).Value + 10
End If

Next count
End Sub

The problem with this code is that myResult returned value is always false (so everything is set to 0 in my array).

Thanks a lot for your help.

EDIT: after trying a suggestion :
1/ Using VBA (working for everything but “Quezon”)
enter image description here
2/ Using formulas… same issue.
enter image description here

How to&Answers:

I want my method probaCity() to look for matches between these two columns. If there is a match, I want it to display some value (10 for example) in the corresponding row of the G column (in the array “Probablity of being the city”), if no match, then it’s gonna be 0.

No Need to use VBA for this.

See this example

Use this Array formula. You will have to press CTRL+SHIFT+ENTER after entering the formula

=IF(COUNT(FIND($C$9:$C$11,A1)),"10","")

Screenshot

enter image description here

EDIT: Since you want a VBA method, try this.

VBA METHOD (this will work with the sample data as per your original post)

Sub Sample()
    Dim ws As Worksheet
    Dim LookUpRange As Range
    Dim lRow As Long

    '~~> Change this to the relevant sheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> This is your city list
        Set LookUpRange = .Range("G21:G25")

        lRow = .Range("C" & .Rows.Count).End(xlUp).Row

        For i = 2 To lRow
            .Range("G" & i).Value = Application.Evaluate("=IF(COUNT(FIND(" & _
                                    LookUpRange.Address & _
                                    "," & .Range("C" & i).Address & ")),""10"",""0"")")
        Next i
    End With
End Sub

Answer:

The problem with this code is that myResult returned value is always false (so everything is set to 0 in my array).

Did you make sure that myResult is always false, or have you deduced it from the 0 values displayed?

Not sure if it will help, but IsError feels more natural than IsNumeric here. Your original If statement would be in the right order.

Answer:

No need for VBA.

  1. Type formula =IF(ISNA(MATCH(C2,$G$21:$G$25,0)),0,10) into G2.
  2. Copy G2 to G3:G17.