Index and partial match with multiple criteria across two sheets

rrands1

New Member
Joined
Nov 7, 2004
Messages
29
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hi all - I have two workbooks that I am trying to do partial matches across, but have gotten totally lost, so rather than post what I have tried, I thought it more expedient to post what I am after. Any help is greatly appreciated!

brief description of sheets:
Sheet1:
Contains columns of Latitude, Longitude, and "Type" (Type can be House or Business)
Sheet2:
Contains columns of similar Latitude, Longitude, and an empty "Type" column that I want to populate. The degree of precision in the Latitude and Longitude data is different than Sheet 1, so while there are enough digits to get it close enough, it is not an exact match with Sheet1's data (thus the need for partial match)

Background & Goal:
Using Excel 2019 (if that matters)
For latitude, I want to match on the first (left-most) 6 digits (i.e. 33.39.) (sheet 1 as the source)
For longitude, I want to match on the first (left-most) 9 digits (i.e. -111.6363) (sheet 1 as the source)
When a partial match on both is found, I want to take the value from "Type" in Sheet 1, and copy it to the "Type" column in Sheet 2

I have gotten the code to loop through each line in Sheet 1 working OK, but I am getting hung up on the Index and Match part, as it does not seem to be matching on the partial, even with appending a "*" at the end of it.

Below are two mini-sheets (for Sheet1 and Sheet2, respectively):
residential 74 - exported from TH.xlsx
ABC
1Location typeLatitudeLongitude
2House33.39484879-111.6363394
3House33.39493249-111.6410672
4House33.3953966-111.6411081
5House33.39492689-111.6405243
6House33.39543915-111.6404992
7House33.39486475-111.6399115
8House33.39550353-111.6399654
9House33.39484459-111.6395165
10House33.3954537-111.6389164
11House33.39485635-111.6388211
12House33.39543551-111.6385087
13House33.39491961-111.6383809
14House33.39479029-111.6379159
15House33.39550996-111.6378944
16House33.39485803-111.6373892
17House33.39546462-111.6372463
18House33.39472535-111.636848
19House33.39546574-111.6368678
20House33.39548533-111.6361902
21House33.39485075-111.6357628
22House33.39552592-111.6358855
23House33.39544446-111.6351294
24House33.39484823-111.6352692
25House33.39472339-111.6345866
26House33.39549961-111.6347103
27House33.39487203-111.6341903
28House33.39367816-111.6389331
29House33.39377837-111.638329
30House33.39381056-111.6378854
31House33.39373134-111.637304
32House33.39665818-111.6405585
33House33.39666378-111.6400067
34House33.39667218-111.6394461
35House33.3966621-111.6389254
36House33.3966621-111.6384657
37House33.39669457-111.6379032
38House33.39671557-111.6373634
39House33.39669513-111.6368226
40House33.39668282-111.6363186
41House33.39668954-111.635803
42House33.39668478-111.6352773
Sheet1


residential 74 - exported from TH.xlsx
ABC
1TypeLatitudeLongitude
233.39649178-111.6410622
333.39648566-111.6339996
433.39648616-111.6352743
533.39470048-111.6410661
633.39566599-111.6410639
733.39470271-111.6405401
833.39566621-111.6405401
933.39470114-111.6400118
1033.39566547-111.6400147
1133.39470107-111.6394848
1233.3956636-111.6389577
1333.39470108-111.6389275
1433.39566453-111.6384313
1533.39470024-111.6384002
1633.39470024-111.6379068
1733.39566437-111.6379057
1833.39469984-111.6373837
1933.39566324-111.6373828
2033.39469892-111.6368586
2133.3956629-111.6368572
2233.39469855-111.6363304
2333.39566281-111.6363279
2433.39469844-111.6358026
2533.39566212-111.6358018
2633.39469746-111.6352749
2733.39566192-111.6352786
2833.39469739-111.6347495
2933.39566098-111.6347549
3033.39469737-111.6342268
3133.39490662-111.6332597
3233.39490714-111.6337459
3333.39387604-111.6408059
3433.39387341-111.6389652
3533.3938736-111.6384388
3633.39387212-111.6379113
3733.39387244-111.6373889
3833.39388985-111.6352764
3933.39388854-111.6347493
4033.39649167-111.6405385
4133.3964911-111.6400119
4233.39649068-111.639484
4333.39649109-111.6389568
4433.39648944-111.6384298
4533.3964885-111.6379036
4633.39648883-111.6373802
4733.39648829-111.6368538
4833.39648692-111.6363243
4933.39648721-111.6357978
Sheet2


Note that the data in the Lat/Lon may be longer than what is actually shown in the cell (however if you click on the cell, you will see the entire coordinate). Also, there are more rows in Sheet2 than Sheet1, but that is OK for my purposes.

Thank you again - I appreciate any help! I am going to continue to plug away at it, but will update if I get it working the mean time!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I've numbered the Houses to reference the return values, does this give the intended results?

Book1
ABC
1Location typeLatitudeLongitude
2House133.39484879-111.6363394
3House233.39493249-111.6410672
4House333.3953966-111.6411081
5House433.39492689-111.6405243
6House533.39543915-111.6404992
7House633.39486475-111.6399115
8House733.39550353-111.6399654
9House833.39484459-111.6395165
10House933.3954537-111.6389164
11House1033.39485635-111.6388211
12House1133.39543551-111.6385087
13House1233.39491961-111.6383809
14House1333.39479029-111.6379159
15House1433.39550996-111.6378944
16House1533.39485803-111.6373892
17House1633.39546462-111.6372463
18House1733.39472535-111.636848
19House1833.39546574-111.6368678
20House1933.39548533-111.6361902
21House2033.39485075-111.6357628
22House2133.39552592-111.6358855
23House2233.39544446-111.6351294
24House2333.39484823-111.6352692
25House2433.39472339-111.6345866
26House2533.39549961-111.6347103
27House2633.39487203-111.6341903
28House2733.39367816-111.6389331
29House2833.39377837-111.638329
30House2933.39381056-111.6378854
31House3033.39373134-111.637304
32House3133.39665818-111.6405585
33House3233.39666378-111.6400067
34House3333.39667218-111.6394461
35House3433.3966621-111.6389254
36House3533.3966621-111.6384657
37House3633.39669457-111.6379032
38House3733.39671557-111.6373634
39House3833.39669513-111.6368226
40House3933.39668282-111.6363186
41House4033.39668954-111.635803
42House4133.39668478-111.6352773
Sheet1


Book1
ABC
1TypeLatitudeLongitude
2 33.39649178-111.641062
3 33.39648566-111.634
4House4133.39648616-111.635274
5House233.39470048-111.641066
6 33.39566599-111.641064
7House433.39470271-111.64054
8 33.39566621-111.64054
9 33.39470114-111.640012
10 33.39566547-111.640015
11 33.39470107-111.639485
12House933.3956636-111.638958
13 33.39470108-111.638928
14 33.39566453-111.638431
15 33.39470024-111.6384
16House1333.39470024-111.637907
17 33.39566437-111.637906
18House1533.39469984-111.637384
19 33.39566324-111.637383
20House1733.39469892-111.636859
21House1833.3956629-111.636857
22House133.39469855-111.63633
Sheet2
Cell Formulas
RangeFormula
A2:A22A2=IFNA(INDEX(Sheet1!$A$2:$A$42,MATCH((LEFT(B2,6)&LEFT(C2,9)),INDEX(LEFT(Sheet1!$B$2:$B$42,6)&LEFT(Sheet1!$C$2:$C$42,9),),0)),"")
 
Upvote 0
Hi, @RasGhul - let me go take a poke at it - I see I left out an important piece, and I am sorry for that - I need to do this via VBA. Does that change things at all?

I *think* I have determined that the issue might be that my range coming back is numeric, however adding the "*" to the value to compare against (in order to get the partial match) is making it Text. However, I have been unable to get the Range statement in VBA to treat the range as text. What I am trying:
Sub test()
sLat = Left(ActiveSheet.Range("B2").Value, 6) & "*"
Rng = ThisWorkbook.Worksheets("Sheet2").Range("B2:B49")
ActiveSheet.Range("E48").Value = Application.Match(sLat, Rng, 0) '' this always comes back as "N/A", meaning it didn't find anything, though it should have...
End Sub



Thank you again - let me go try your answer as well & see if it gives me any clues! I appreciate the help!!

-randy
 
Upvote 0
What type should be taken if, there were duplicate in Lat & Long? For instant, row 7 & 9 sheet 1:
33.39486 - 111.64 ===> to be 33.394 - 111.640
33.39484 - 111.64 ===> to be 33.394 - 111.640

Do you want to take full Lat to get the different, in case 6 left digits is duplicated?
 
Upvote 0
Regardless to duplicate,
Intead of left, since Lat and Long is value, try using rounddown like this
for instant, first row of sheet1: 33.3948487933922 -111.636339426041 to become 33.394-111.636
VBA Code:
Sub test()
Dim Jarr As Variant
Dim i, Lr As Long
Lr = Sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row
ReDim Jarr(1 To Lr - 1, 1 To 2)
With WorksheetFunction
For Each cell In Sheets("Sheet1").Range("B2:B" & Lr)
    i = i + 1
    Jarr(i, 1) = .RoundDown(cell, 3) & .RoundDown(cell.Offset(, 1), 3)
    Jarr(i, 2) = cell.Offset(, -1)
Next
    For Each cell In Sheets("Sheet2").Range("B2:B" & Cells(Rows.Count, "B").End(xlUp).Row)
        For i = 1 To UBound(Jarr)
            If .RoundDown(cell, 3) & .RoundDown(cell.Offset(, 1), 3) = Jarr(i, 1) Then
                cell.Offset(, -1).Value = Jarr(i, 2)
                Exit For
            End If
        Next
    Next
End With
End Sub
 
Upvote 0
Regardless to duplicate,
Intead of left, since Lat and Long is value, try using rounddown like this
for instant, first row of sheet1: 33.3948487933922 -111.636339426041 to become 33.394-111.636
VBA Code:
Sub test()
Dim Jarr As Variant
Dim i, Lr As Long
Lr = Sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row
ReDim Jarr(1 To Lr - 1, 1 To 2)
With WorksheetFunction
For Each cell In Sheets("Sheet1").Range("B2:B" & Lr)
    i = i + 1
    Jarr(i, 1) = .RoundDown(cell, 3) & .RoundDown(cell.Offset(, 1), 3)
    Jarr(i, 2) = cell.Offset(, -1)
Next
    For Each cell In Sheets("Sheet2").Range("B2:B" & Cells(Rows.Count, "B").End(xlUp).Row)
        For i = 1 To UBound(Jarr)
            If .RoundDown(cell, 3) & .RoundDown(cell.Offset(, 1), 3) = Jarr(i, 1) Then
                cell.Offset(, -1).Value = Jarr(i, 2)
                Exit For
            End If
        Next
    Next
End With
End Sub
Thank you, @bebo021999 and @RasGhul - I didn't realize that I would get multiple hits doing it that way, as I assumed that I had enough digits in the geocode to make them unique enough for my purposes.. So, I think I will try and do some address matching, and I have been giving that a go for a while, but am stuck on something - rather than add to this, I think I will open a new post so it is (hopefully) clear. I very much appreciate your time and input - thank you!

-randy
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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