I am trying to write a function that calculates the minimum distance of one zip code to the other. The function should take in the longitude and latitude of one zip code, and then a 2-D array with all longitude and latitude info of zip codes. Here is the function I wrote:
Public Function PassArray(Longitude As Double, Latitude As Double, ParamArray varValues() As Variant) As Double
Dim arr() As Variant
Dim x As Long
For x = 1 To UBound(varValues(0), 1)
ReDim Preserve arr(x)
arr(UBound(arr)) = Sqr((Longitude - varValues(0)(x, 1)) ^ 2 + (Latitude - varValues(0)(x, 2)) ^ 2)
Next x
PassArray = WorksheetFunction.Min(arr)
I got #Value! error when I tried to use this function. I checked every step and it seems that the UBound(varValues(0), 1) is causing the problem. When I try UBound(varValues) it returns 0, which I guess it is the first dimension upper bound of the parameter array?
I cannot get why UBound(varValues(0), 1) would not work. I thought it should return my longitude & latitude array's last row number.
Consider @Mathieu Guindon's comment and go along those lines:
Note that I can't vouch for your distance calculation; might work for cartesian coordinates, but for not for longitude/latitude.