I am trying to write a formula that takes into consideration 'n' amount of customers at 'x' address and how much they order ('q'). I would like for the formula to then print out the latitude/longitude of the best location that the 'centroid' warehouse should be.
I would prefer for it to be a command such as =getCentroid
.
Thanks for any help.
EDIT
Since some people might think that this is too broad or does not have enough information -- I will provide an old code I have.
This code takes the latitudes and longitudes that I enter, and then considers the number of shipments and then proceeds to tell me where the new warehouse should be. It is so old, that I am not sure how it works.
Private Sub CommandButton1_Click()
Dim i As Integer
Dim j As Integer
Dim count As Integer
i = 3
j = 0
count = 3
dtr = 0.0174533 'degrees to radians calculation
RTD = 57.2958 'radians to degrees
LatFactor = 69.172 'miles in 1 degree change in lat
'Finds how many locations there are around whs as j
Do While Cells(i, 2) <> ""
j = j + 1
lats = lats + Cells(i, 2)
Longs = Longs + Cells(i, 3)
i = i + 1
Loop
'Create arrays of lats and longs starting at 0
Dim lat() As Variant
ReDim lat(0 To j)
Dim lon() As Variant
ReDim lon(0 To j)
For x = 1 To j
lat(count - 3) = Cells(count, 2)
lon(count - 3) = Cells(count, 3)
count = count + 1
Next
R = 3959 'Radius of earth
whsLat = Cells(2, 2) 'Lattitude of Whs in NOT Radians
whsLon = Cells(2, 3) 'Lattitude of whs NOT in rads
whsLatr = Cells(2, 2) * dtr
whsLonr = Cells(2, 3) * dtr
'Calculates distance from warehouse to location 1 as d
'uses haversine formula-as crow flies
Dim Distances() As Variant
ReDim Distances(0 To j)
For x = 1 To j
Clat = lat(x - 1) * dtr
deltaLat = (lat(x - 1) - whsLat) * dtr
deltaLon = (lon(x - 1) - whsLon) * dtr
a = (Math.Sin(deltaLat / 2) * Math.Sin(deltaLat / 2)) +
(Math.Cos(whsLatr) * Math.Cos(Clat) * Math.Sin(deltaLon / 2) *
Math.Sin(deltaLon / 2))
c = 2 * Math.Atn((Math.Sqr(a) / Math.Sqr(1 - a)))
d = R * c
Distances(x - 1) = d 'distance values
Cells(x + 2, 13) = d
Next
TotalMiles = WorksheetFunction.Sum(Distances)
step = 1
'Calculate optimum location using halves
Olat = lat(0)
Olon = lon(0)
OLatr = lat(0) * dtr
OLonr = lon(0) * dtr
Dlat = lat(1)
DLatr = lat(1) * dtr
Dlon = lon(1)
Dlonr = lon(1) * dtr
LatChange = (lat(1) - Olat) * dtr
LonChange = (lon(1) - Olon) * dtr
'Counting Variables for weight
y = 3
Z = 4
ShipSum = Cells(y, 4) + Cells(Z, 4)
For x = 1 To j - 1
anew = (Math.Sin(LatChange / 2) * Math.Sin(LatChange / 2)) +
(Math.Cos(OLatr) * Math.Cos(DLatr) * Math.Sin(LonChange / 2) *
Math.Sin(LonChange / 2))
cnew = 2 * Math.Atn((Math.Sqr(anew) / Math.Sqr(1 - anew)))
dnew = R * cnew
'Calculate new lat and long
hyp = dnew / 2 ' Total distance moved
adj = Abs(LatFactor * (Dlat - Olat)) 'y distance
Degree = WorksheetFunction.Acos(adj / dnew * dtr) 'degree from 90
If (Dlat - Olat) > 0 Then NewLat = Olat + (Cells(Z, 4) / (ShipSum)) *
Abs(hyp / LatFactor * Math.Cos(Degree) * RTD) 'New lattitude if going up
If (Dlat - Olat) < 0 Then NewLat = Olat - (Cells(Z, 4) / (ShipSum)) *
Abs(hyp / LatFactor * Math.Cos(Degree) * RTD) 'New Lattitude if going down
Opp = (Dlon - Olon) * Math.Cos(NewLat * dtr) 'x distance adjusted for polar
flattening
If (Dlon - Olon > 0) Then NewLon = Olon + (Cells(Z, 4) / (ShipSum)) *
Abs(Opp) 'new long
If (Dlon - Olon < 0) Then NewLon = Olon - (Cells(Z, 4) / (ShipSum)) *
Abs(Opp)
Olat = NewLat 'Setting new origin
Olon = NewLon
OLatr = NewLat * dtr
OLonr = NewLon * dtr
If x < j Then
Dlat = lat(x + 1) 'If there is another iteration, set new destination
DLatr = lat(x + 1) * dtr
Dlon = lon(x + 1)
Dlonr = lon(x + 1) * dtr
LatChange = (lat(x + 1) - Olat) * dtr
LonChange = (lon(x + 1) - Olon) * dtr
y = y + 1
Z = Z + 1
ShipSum = ShipSum + Cells(Z, 4)
End If
Next
Cells(3, 8) = NewLat
Cells(3, 9) = "-" & NewLon
whsLat = NewLat 'Lattitude of New Whs in NOT Radians
whsLon = NewLon 'Lattitude of whs NOT in rads
whsLatr = NewLat * dtr
whsLonr = NewLon * dtr
'Calculates distance from warehouse to location 1 as d
'uses haversine formula-as crow flies
Dim NewDistances() As Variant
ReDim NewDistances(0 To j)
For x = 1 To j
Clat = lat(x - 1) * dtr
deltaLat = (lat(x - 1) - whsLat) * dtr
deltaLon = (lon(x - 1) - whsLon) * dtr
a = (Math.Sin(deltaLat / 2) * Math.Sin(deltaLat / 2)) +
(Math.Cos(whsLatr) * Math.Cos(Clat) * Math.Sin(deltaLon / 2) *
Math.Sin(deltaLon / 2))
c = 2 * Math.Atn((Math.Sqr(a) / Math.Sqr(1 - a)))
d = R * c
Cells(x + 2, 10) = d
NewDistances(x - 1) = d 'distance values
Next
NewTotalMiles = WorksheetFunction.Sum(NewDistances)
Cells(j + 3, 10) = NewTotalMiles
Worksheets("Sheet1").Range("K3:K100").ClearContents
i = 3
Do While i < 44
Cells(i, 11) = Cells(i, 10) * Cells(i, 4)
i = i + 1
Loop
Cells(11, 11) = Cells(3, 11) + Cells(4, 11) + Cells(5, 11) + Cells(6, 11) +
Cells(7, 11) + Cells(8, 11) + Cells(9, 11) + Cells(10, 11)
End Sub
=getCentroid
) you will have to 1) carefully formulate your problem (with much more detail than you give in the question), 2) decide on an algorithm to solve it, at least heuristically, and 3) implement your algorithm in VBA. Perhaps you can edit your question so that at least1
above is done.lats = lats + Cells(i, 2)
-- why add latitudes together? There is no geometric motivation for doing so and the variablelats
isn't even used anywhere in the rest of the code). I would consider throwing the code away and starting from scratch. It shouldn't be too hard to find pseudocode for computing the centroid of a discrete mass distribution on the surface of a sphere (where presumably the mass at a given location is the quantity there) and it shouldn't be too hard to code it in VBA. Might be easier than trying to decipher old code.