Return location match/matches in a radius.

SlightlyClueless

New Member
Joined
Dec 10, 2018
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hello All,

For the past day now I have been working on a solution to return locations that fall within a 1/4 mile and 1/2 mile radius of a specific site. There is a list of about 3,000 sites that would need to be bounced off a portfolio (10,000+ locations) to see if they fell within a 1/4 mile and 1/2 mile radius of a location/locations in the portfolio. All the location data is Decimal Degrees format. In order to get things working properly first, I made a small mock data set to work with. A link to google drive to download that workbook is below. So far I have been able to get a site to return the closest match from the portfolio and the distance the site is from the portfolio match. However, in the real dataset along with the test data set, there may be more than one match from the portfolio. What do is needed to be done in order for each site to list the match/matches from the portfolio that fall in a 1/4 mile and 1/2 mile radius?

Site List
Sites.jpg


Distance formula

Excel Formula:
=INDEX(ACOS(COS(RADIANS(90-Portfolio!$C$2:$C$6)) *COS(RADIANS(90-C2)) +SIN(RADIANS(90-Portfolio!$C$2:$C$6)) *SIN(RADIANS(90-C2)) *COS(RADIANS(Portfolio!$D$2:$D$6-D2)))*3958.756,MATCH(SMALL((ABS(C2-Portfolio!$C$2:$C$6)^2+ABS(D2-Portfolio!$D$2:$D$6)^2)^(0.5),1),(ABS(C2-Portfolio!$C$2:$C$6)^2+ABS(D2-Portfolio!$D$2:$D$6)^2)^(0.5),0))

Nearest Location formula

Excel Formula:
=INDEX(Portfolio!$A$2:$A$6,MATCH(SMALL((ABS(C2-Portfolio!$C$2:$C$6)^2+ABS(D2-Portfolio!$D$2:$D$6)^2)^(0.5),1),(ABS(C2-Portfolio!$C$2:$C$6)^2+ABS(D2-Portfolio!$D$2:$D$6)^2)^(0.5),0))


Portfolio

portfolio.jpg



Google Drive Link
VBA Code:
https://drive.google.com/uc?export=download&id=1eaDyUrjGjonV6iJkMa1Tac2v6NfquIsx
 

Attachments

  • Sites.jpg
    Sites.jpg
    97.9 KB · Views: 8
  • portfolio.jpg
    portfolio.jpg
    99.2 KB · Views: 10

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
The links do not seem to be working. What version of Excel are you using?
 
Upvote 0
The links do not seem to be working. What version of Excel are you using?
Sorry about that! I had turned the link into a direct download link so that it wouldn't load right into google sheets. The regular link to the sheet is
Excel Formula:
https://docs.google.com/spreadsheets/d/1eaDyUrjGjonV6iJkMa1Tac2v6NfquIsx/edit?usp=sharing&ouid=102720600064113628014&rtpof=true&sd=true

Office 365 for business is installed on this PC
 
Upvote 0
I would suggest something like this...
Convert your data on the Portfolio sheet to an official Excel table (click anywhere in data range and then Ctrl-t and confirm that you have table headers. Note that I added a location far away to confirm that it is being correctly filtered in the next step. I also named this table tblPortfolio. The advantage here is that tables can expand/shrink and the ranges referenced do not require further adjustments because the formula uses structured references (refers to column heading names).
Test Location Book v1.xlsx
ABCD
1Site IDSite NameLatitudeLongitude
2Portfolio B-1Dental26.00755-80.248754
3Portfolio B-2Hospital26.03045-80.246743
4Portfolio B-3Home Depot25.99561-80.203481
5Portfolio B-4Hard Rock26.05504-80.211548
6Portfolio B-5Publix26.01383-80.250659
7Port C-1Pub35-90
Portfolio

Then to eliminate redundancies in the formulas and to more efficiently find those that meet your distance criteria, use a variety of functions available in Excel 365.
I've modified the formula used in the results table to offer some clarity about what the formula does. The results in the lower explanatory table show what is happening behind the scenes for the green cell above. In particular, we create an array of distances and then horizontally append that array to the main data array (done by HSTACK). Then we can filter this array to extract only those rows where the distance criterion is met. Finally, we can sort this array by distance and then return only the locations associated with those distances. The lower formula is not needed.
Test Location Book v1.xlsx
ABCDEF
10.250.5
2Site IDSite NameLatitudeLongitudeLocations within distanceLocations within distance
3Site 1Goodwill26.01108058-80.25058174Publix, Dental, Hospital, Home Depot, Hard RockPublix, Dental, Hospital, Home Depot, Hard Rock
4Site 2Lowes26.01149703-80.25072055Publix, Dental, Hospital, Home Depot, Hard RockPublix, Dental, Hospital, Home Depot, Hard Rock
5Site 3Fictitious35.01-90.01PubPub
6
7Portfolio B-5Publix26.01383088-80.250659130.002751383
8Portfolio B-1Dental26.00754567-80.248754230.003979368
9Portfolio B-2Hospital26.03044507-80.246742830.019741341
10Portfolio B-3Home Depot25.99561025-80.203481490.049575848
11Portfolio B-4Hard Rock26.05503966-80.211547880.05878811
Sites
Cell Formulas
RangeFormula
E3:F5E3=LET(dist,(($C3-tblPortfolio[Latitude])^2+($D3-tblPortfolio[Longitude])^2)^0.5,TEXTJOIN(", ",,CHOOSECOLS(SORT(FILTER(HSTACK(tblPortfolio,dist),dist<=E$1),5),2)))
B7:F11B7=LET(dist,(($C3-tblPortfolio[Latitude])^2+($D3-tblPortfolio[Longitude])^2)^0.5,res,SORT(FILTER(HSTACK(tblPortfolio,dist),dist<=E$1),5),res)
Dynamic array formulas.
 
Upvote 1
This distance formula (dist) can be replaced by any of several spherical distance formulas if preferred. As you'll note, the simpler Pythagorean approach is used in my formulation, and it probably offers sufficient accuracy for such short distances where "flat Earth" assumptions are reasonable.
 
Upvote 1
Sorry...I was more focused on working with the arrays and not paying attention to the distance formula. What I had is not correct for distance...I'm assuming you are happy with the ACOS formula for distance. If so, then here it is incorporated into my earlier offering. I've changed the green cell so that you can see the array returned by the formula (which in this case has two entries).
Test Location Book v1.xlsx
ABCDEF
10.250.5
2Site IDSite NameLatitudeLongitudeLocations within distanceLocations within distance
3Site 1Goodwill26.01108058-80.25058174PublixPublix, Dental
4Site 2Lowes26.01149703-80.25072055PublixPublix, Dental
5Site 3Fictitious35.001-90.001PubPub
6
7Portfolio B-5Publix26.01383088-80.250659130.190087745
8Portfolio B-1Dental26.00754567-80.248754230.269314575
Sites
Cell Formulas
RangeFormula
E3:F5E3=LET(lat,tblPortfolio[Latitude],long,tblPortfolio[Longitude], dist,ACOS(COS(RADIANS(90-lat)) *COS(RADIANS(90-$C3)) +SIN(RADIANS(90-lat)) *SIN(RADIANS(90-$C3)) *COS(RADIANS(long-$D3)))*3958.756, TEXTJOIN(", ",,CHOOSECOLS(SORT(FILTER(HSTACK(tblPortfolio,dist),dist<=E$1),5),2)))
B7:F8B7=LET(lat,tblPortfolio[Latitude],long,tblPortfolio[Longitude], dist,ACOS(COS(RADIANS(90-lat)) *COS(RADIANS(90-$C3)) +SIN(RADIANS(90-lat)) *SIN(RADIANS(90-$C3)) *COS(RADIANS(long-$D3)))*3958.756, SORT(FILTER(HSTACK(tblPortfolio,dist),dist<=F$1),5))
Dynamic array formulas.
 
Upvote 1
Solution
I'm happy to help. By appending the distance array to the data as a first step (using HSTACK), we can then extract relevant information and process it further in the other steps...in this case FILTER those meeting the distance threshold and then SORT, with the closer distances listed first. You could also return the estimated "straight-line" distances together with the locations, but I wasn't sure about your final preferred outputs...sounds as if TEXTJOIN of locations in a single cell is desired.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

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