I am trying to write code for Excel 2007 that will calculate a variable which sums the sales of all competing businesses within a 2 mile radius. I want to do this for 7,678 businesses in my dataset. The formula to evaluate the distances between businesses uses latitude and longitude points - I have tested this formula using two businesses (the formula used in my 'if -then' statement) and it is accurate. I am trying, however, to write code to evaluate the entire array of businesses for each individual data point to create this new field for all observations.
I have been getting different errors and tweaking my code to fix them, but I have come to a point where I need help. My last error is a run-time error '438' - "object doesn't support this property or method'. I will paste my code below (the error refers to my 'If' statement). i and j are the rows = business units, column A has latitudes, B has longitudes, F is sales and H will have my new variable (total sales of businesses within 2 miles).
Sub Compet()
Dim i As Integer
Dim j As Integer
Dim RangeSum As Long
RangeSum = 0
i = 2
j = 2
Do Until j > 7678
Do Until i > 7678
Worksheets("infotest").Select
If (Application.WorksheetFunction.Acos(Application.WorksheetFunction.Sin((3.14 159 / 180) * Range("A" & j).Value) _
* Application.WorksheetFunction.Sin((3.14159 / 180) * Range("A" & i).Value) + Application.WorksheetFunction.Cos((3.14159 / 180) _
* Range("A" & j).Value) * Application.WorksheetFunction.Cos((3.14159 / 180) * Range("A" & i).Value) _
* (Application.WorksheetFunction.Cos((3.14159 / 180) * Range("B" & j).Value - (3.14159 / 180) _
* Range("B" & i).Value))) * 3959) < 2 Then
RangeSum = RangeSum + Worksheets("infotest").Range("F" & i).Value
End If
i = i + 1
Loop
Worksheets("infotest").Range("h" & j).Select
ActiveCell.Print RangeSum
RangeSum = 0
i = 2
j = j + 1
Loop
End Sub
sorry my formula is sloppy - the indenting didn't come out like the module.
any help is much appreciated!!