Exclude cells that has a value that start with specific letters

Brutusar

Board Regular
Joined
Nov 23, 2019
Messages
166
Office Version
  1. 365
Platform
  1. Windows
Hi, I am using the code below to find matching city names in two different sheets, A and B. Sheet A col A has over 100 000 city names, and a corresponding code in col B. In sheet B col A I enter multiple city names, run the code that find the corresponding city in sheet A and copy the code to sheet B col B.

The problem is that, especially in the USA, many cities have names that are identical in other countries. That causes the macro to show the wrong code because the wrong "instance" of the city is chosen. Is there a way to rewrite the code I am using to it is possible to deselect one or more countries from a dropdown list, so those countries are not being searched? The country code is always the two first letters in sheet A col B. So if searching for New York but not in the USA (yes, it is existing), then US should be deselected, and then USA will not be searched?



VBA Code:
Sub Find_Similar()
    Dim d As Object, v As Variant, i As Long

    Set d = CreateObject("Scripting.Dictionary")
    
    v = Sheets("A").Cells(1, 1).CurrentRegion.Resize(, 2)
    For i = LBound(v) To UBound(v)
        d(v(i, 2)) = v(i, 1)
    Next i
    
    v = Sheets("B").Cells(1, 1).CurrentRegion.Resize(, 2)
    For i = LBound(v) To UBound(v)
        If d.exists(v(i, 1)) Then v(i, 2) = d(v(i, 1))
    Next i
    
    Sheets("B").Cells(1, 1).CurrentRegion.Resize(, 2) = v
End Sub
 
That works perfectly. Way out of my coding league!

Thank you very much for your time and work!
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,542
Members
449,316
Latest member
sravya

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