Compare data within same cell

Psyanide

New Member
Joined
May 5, 2016
Messages
8
I require a macro code which compares data within same cell and accordingly provides status either "match found" or "mismatch"
I work on a discrepancy report and most of the property addresses are same only difference is abbreviations
eg. street = st, Road = rd and so on.

In the below example data on left side of "|||" is from 1 source and data on right side of "|||" is from other source
In the below example "A2" and "A3" are good to be deleted however "A4" has "drive" against "avenue" hence it needs to be verified.
Can you please provide me a macro code which would give me output as presented in "B" column based on the data on column "A"

Note: There are many such abbreviations I come across which I will add in the code by myself once I have the basic code.


AB
1Property AddressMacro result
2Apple Street ||| Apple StDelete
3Blue Road colony ||| Blue Rd colonyDelete
4Sunshine Drive ||| Sunshine AvenueCheck

<colgroup><col><col><col></colgroup><tbody>
</tbody>


Thanks :)
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Assuming they are all formatted like that (separated by the "| | |" and the longer name on the left), this will work:

Code:
Public Function address1(str As String) As String
    address1 = Left(str, Application.Find("|||", str) - 2)
End Function
 
Public Function address2(str As String) As String
    address2 = Right(str, Len(str) - Application.Find("|||", str) - 3)
End Function

Sub Button1_Click()
    Dim add1 As String
    Dim add2 As String
    Dim currRow As Long
    Dim maxRow As Long
    Dim address As String
    
    maxRow = 4 ' However many rows you have
    
    For currRow = 1 To maxRow
        address = Cells(currRow, 1).Value
        add1 = address1(address)
        add2 = address2(address)
        If LCase(add1) = Replace(LCase(add2), "st", "street") Then 'Do the same for road, rd, etc.
            Cells(currRow, 2).Value = "Delete"
        Else
            Cells(currRow, 2).Value = "Verify"
        End If
    Next
End Sub
 
Upvote 0
This works thanks.
Only 1 small addition is required.
You are right with the assumption that data is always separated by the " ||| " sign.

However,

1 - The longer name could be on right and shorter name on left and vice versa.

2 - There could be more than one (2,3,4....) short and long names in an address e.g. "Apple Street Drive ||| Apple St Dr" or "Apple St Drive Road ||| Apple Street Dr Rd"......this is random.


Can you modify the code to handle these scenarios ?
 
Upvote 0
I figured out the first scenario.

Please help me with the second scenario where the address is having more than one abbreviation.

Thanks :)
 
Upvote 0
Add this function:

Code:
Public Function replaceAll(str As String) As String
    str = Replace(str, "st", "street")
    str = Replace(str, "ave", "avenue")
    str = Replace(str, "rd", "road") 'Add as many of these as you want
    
    replaceAll = str
End Function

And then just change the if statement to this:

Code:
If LCase(add1) = replaceAll(LCase(add2)) Or LCase(add2) = replaceAll(LCase(add1)) Then
 
Upvote 0
Little messed up with complaining all the code. Please assist what should be retained and what should be deleted from the final code........



Public Function address1(str As String) As String
address1 = Left(str, Application.Find("|||", str) - 2)
End Function

Public Function address2(str As String) As String
address2 = Right(str, Len(str) - Application.Find("|||", str) - 3)
End Function
Public Function replaceAll(str As String) As String
str = Replace(str, "st", "street")
str = Replace(str, "ave", "avenue")
str = Replace(str, "rd", "road") 'Add as many of these as you want

replaceAll = str
End Function


Sub Button1_Click()
Dim add1 As String
Dim add2 As String
Dim currRow As Long
Dim maxRow As Long
Dim address As String

maxRow = 4 ' However many rows you have

For currRow = 1 To maxRow
address = Cells(currRow, 1).Value
add1 = address1(address)
add2 = address2(address)
If LCase(add1) = replaceAll(LCase(add2)) Or LCase(add2) = replaceAll(LCase(add1)) Then 'Do the same for road, rd, etc.
Cells(currRow, 2).Value = "Delete"
Else
Cells(currRow, 2).Value = "Verify"
End If
Next
End Sub
 
Upvote 0
I require solution for case 1

1- Apple Road St ||| Apple Rd Street Verify
2- Apple St ||| Apple StreetDelete
3 - Apple Road ||| Apple RdDelete
4 - Apple Avenue ||| Apple AveDelete

<colgroup><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
In order to handle that, change the replaceAll function to go from long names to short names, and then apply it to both sides in the if statement.

Full code:

Code:
Public Function address1(str As String) As String
    address1 = Left(str, Application.Find("|||", str) - 2)
End Function
 
Public Function address2(str As String) As String
    address2 = Right(str, Len(str) - Application.Find("|||", str) - 3)
End Function

Public Function replaceAll(str As String) As String
    str = Replace(str, "street", "st")
    str = Replace(str, "avenue", "ave")
    str = Replace(str, "road", "rd") 'Add as many of these as you want
    
    replaceAll = str
End Function

Sub Button1_Click()
    Dim add1 As String
    Dim add2 As String
    Dim currRow As Long
    Dim maxRow As Long
    Dim address As String
    
    maxRow = 5 ' However many rows you have
    
    For currRow = 1 To maxRow
        address = Cells(currRow, 1).Value
        add1 = address1(address)
        add2 = address2(address)
        If replaceAll(LCase(add1)) = replaceAll(LCase(add2)) Then
            Cells(currRow, 2).Value = "Delete"
        Else
            Cells(currRow, 2).Value = "Verify"
        End If
    Next
End Sub
 
Upvote 0
Hi there,

I am facing a small challenge in this.

There is chance where there could be a blank cell between two addresses. When I come across this scenario I get an error. Please assist me ?

E.g.
A1 - Fake Street ||| Fake St
A2 - Greet Road ||| Green Rd
A3 -
A4 -
A5 - Red trail ||| Red Trl
 
Upvote 0

Forum statistics

Threads
1,215,456
Messages
6,124,939
Members
449,197
Latest member
k_bs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top