Sumifs - looking up values within a certain distance of a point

Stirling_confused

New Member
Joined
Jan 9, 2023
Messages
2
I have two sets of data – list A is a list of train stations, with their longitude and latitude. List B is a list of places, with the population and a longitude/latitude. I want to be able to write a formula that takes a particular station, and sums the population in List B within 25miles.



List A:
StationLongitudeLattitudePopulation within 25 miles
Station A
51.49106​
0.121394​
Station B
51.57496​
-3.22983​
Station C
51.64471​
-3.32699​
Station D
51.71506​
-3.44308​
Station E
57.1437​
-2.09869​
Station F
56.05459​
-3.30056​
Station G
52.54396​
-4.05707​
Station H
52.89858​
-4.37418​
Station I
51.81669​
-3.00965​
Station J
53.29457​
-3.58262​
Station K
52.41404​
-4.08189​
Station L
53.75297​
-2.36955​


List B:
DescriptionLattitudeLongitudePopulation
Place A
51.519589​
-0.096277417​
1475​
Place B
50.45758​
-4.7809718​
1359​
Place C
51.632212​
-0.14008798​
1495​
Place D
50.855827​
-0.77556035​
1524​
Place E
50.873154​
0.60325584​
1819​
Place F
51.294288​
-0.74939155​
2342​
Place G
52.485147​
-1.7922926​
1617​
Place H
51.510605​
-0.21576033​
1594​
Place I
51.489449​
-0.14014192​
1362​
Place J
51.144167​
0.90731201​
2253​
Place K
51.389227​
-0.42091316​
1635​
Place L
51.506754​
-0.025943412​
1591​
Place M
51.067315​
-0.33381087​
1782​


I know how to calculate the distance between two points using an ACOS formula, however am a bit lost as to how to do a sumifs that calculates the distance between two points. Any help much appreciated
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I'll take your word on it for the ACOS formula for distance between lats and longs. You can put it in here (I use simple pythogarus).

MrExcelPlayground15.xlsx
ABCDEFGHIJK
2StationLongitudeLattitudePopulation within 25 milesDistance4DescriptionLattitudeLongitudePopulation
3Station A51.491060.12139420489Place A51.519589-0.0962774171475
4Station B51.57496-3.2298319595Place B50.45758-4.78097181359
5Station C51.64471-3.3269917776Place C51.632212-0.140087981495
6Station D51.71506-3.4430817776Place D50.855827-0.775560351524
7Station E57.1437-2.098690Place E50.8731540.603255841819
8Station F56.05459-3.300561617Place F51.294288-0.749391552342
9Station G52.54396-4.0570710071Place G52.485147-1.79229261617
10Station H52.89858-4.374185318Place H51.510605-0.215760331594
11Station I51.81669-3.0096521848Place I51.489449-0.140141921362
12Station J53.29457-3.5826217776Place J51.1441680.9073102012253
13Station K52.41404-4.0818911853Place K51.389227-0.420913161635
14Station L53.75297-2.3695516417Place L51.506754-0.0259434121591
15Place M51.067315-0.333810871782
Sheet8
Cell Formulas
RangeFormula
D3:D14D3=SUMPRODUCT(--(SQRT(($I$3:$I$15-B3)^2+($J$3:$J$15-C3)^2)<$F$2),$K$3:$K$15)


Replace:
SQRT(($I$3:$I$15-B3)^2+($J$3:$J$15-C3)^2)
with what you know.
 
Upvote 0
I wrote up a UDF to calculate the distance, and then I had to adapt it to handle arrays. Here it is:

VBA Code:
Public Function Haversine(lat1 As Double, long1 As Double, lat2 As Variant, long2 As Variant, Optional km As Long = 0)
Dim d1 As Object, d2 As Object, i As Long, x As Variant, lat As Variant, lng As Variant

    Set d1 = CreateObject("Scripting.Dictionary")
    Set d2 = CreateObject("Scripting.Dictionary")
    
    If TypeOf lat2 Is Range Then
        For Each x In lat2.Cells
            d1.Add x, 0
        Next x
        For Each x In long2.Cells
            d2.Add x, 0
        Next x
    ElseIf IsArray(lat2) Then
        For Each x In lat2
            d1.Add x, 0
        Next x
        For Each x In long2
            d2.Add x, j0
        Next x
    Else
        d1.Add lat2, 0
        d2.Add long2, 0
    End If
    
    lat = d1.keys
    lng = d2.keys
    
    For i = 0 To UBound(lat)
        
        a = Evaluate("COS(RADIANS(" & lat(i) - lat1 & "))/2")
        b = Evaluate("COS(RADIANS(" & lat1 & "))*COS(RADIANS(" & lat(i) & "))")
        c = Evaluate("(1-COS(RADIANS(" & lng(i) - long1 & ")))/2")
        d = 12742 * Evaluate("ASIN(SQRT(" & 0.5 - a + b * c & "))")
    
        If km = 1 Then d = d * 0.6213
        lat(i) = d
    Next i
        
    Haversine = WorksheetFunction.Transpose(lat)
    
End Function

Once that was complete, you can use it in essentially the same formula that James provided:

Book1
ABCDEFGHIJK
1StationLongitudeLattitudePopulation within 25 milesDescriptionLattitudeLongitudePopulationDistance
2Station A51.491060.1213949152Place A51.51959-0.09627742147525
3Station B51.57496-3.229830Place B50.45758-4.78097181359
4Station C51.64471-3.326990Place C51.63221-0.140087981495
5Station D51.71506-3.443080Place D50.85583-0.775560351524
6Station E57.1437-2.098690Place E50.873150.603255841819
7Station F56.05459-3.300560Place F51.29429-0.749391552342
8Station G52.54396-4.057070Place G52.48515-1.79229261617
9Station H52.89858-4.374180Place H51.51061-0.215760331594
10Station I51.81669-3.009650Place I51.48945-0.140141921362
11Station J53.29457-3.582620Place J51.144170.907312012253
12Station K52.41404-4.081890Place K51.38923-0.420913161635
13Station L53.75297-2.369550Place L51.50675-0.025943411591
14Place M51.06732-0.333810871782
Sheet2
Cell Formulas
RangeFormula
D1D1="Population within "&K2&" miles"
D2:D13D2=SUMPRODUCT($I$2:$I$14,--(haversine(B2,C2,$G$2:$G$14,$H$2:$H$14,1)<=$K$2))


I was a little surprised to see that most of the distances were over 25 miles, so were not included in the totals. I spot checked some of the results of my UDF with some online calculators, and they match. There could still be a problem with my UDF, but check it out and see what you think.
 
Upvote 0
I'll take your word on it for the ACOS formula for distance between lats and longs. You can put it in here (I use simple pythogarus).

MrExcelPlayground15.xlsx
ABCDEFGHIJK
2StationLongitudeLattitudePopulation within 25 milesDistance4DescriptionLattitudeLongitudePopulation
3Station A51.491060.12139420489Place A51.519589-0.0962774171475
4Station B51.57496-3.2298319595Place B50.45758-4.78097181359
5Station C51.64471-3.3269917776Place C51.632212-0.140087981495
6Station D51.71506-3.4430817776Place D50.855827-0.775560351524
7Station E57.1437-2.098690Place E50.8731540.603255841819
8Station F56.05459-3.300561617Place F51.294288-0.749391552342
9Station G52.54396-4.0570710071Place G52.485147-1.79229261617
10Station H52.89858-4.374185318Place H51.510605-0.215760331594
11Station I51.81669-3.0096521848Place I51.489449-0.140141921362
12Station J53.29457-3.5826217776Place J51.1441680.9073102012253
13Station K52.41404-4.0818911853Place K51.389227-0.420913161635
14Station L53.75297-2.3695516417Place L51.506754-0.0259434121591
15Place M51.067315-0.333810871782
Sheet8
Cell Formulas
RangeFormula
D3:D14D3=SUMPRODUCT(--(SQRT(($I$3:$I$15-B3)^2+($J$3:$J$15-C3)^2)<$F$2),$K$3:$K$15)


Replace:
SQRT(($I$3:$I$15-B3)^2+($J$3:$J$15-C3)^2)
with what you know.
Hi

Thank you very much both for replying. And apologies for the delay in responding.

For some reason i can't quite get this to work with my formula: =ACOS( SIN(lat1)*SIN(lat2) + COS(lat1)*COS(lat2)*COS(lon2-lon1) ) * 3959

Is this an inappropriate formula to use with an array?
 
Upvote 0
The formula works with arrays. But here is the output I'm getting comparing the first Station with all of the Places: I don't know what units these are supposed to be. They don't seem to coorespond to something l understand.


5456.982​
1117.086​
5584.376​
7328.369​
2971.508​
6605.898​
3194.528​
5599.724​
5491.8​
3177.236​
5914.655​
5359.269​
5842.516​
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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