Excel 对ParamArray使用UBound时出现值错误

Excel 对ParamArray使用UBound时出现值错误,excel,vba,paramarray,Excel,Vba,Paramarray,我正在尝试编写一个函数来计算一个邮政编码到另一个邮政编码的最小距离。该函数应该获取一个邮政编码的经度和纬度,然后是一个包含邮政编码的所有经度和纬度信息的二维数组。以下是我编写的函数: Public Function PassArray(Longitude As Double, Latitude As Double, ParamArray varValues() As Variant) As Double Dim arr() As Variant Dim x As Long

我正在尝试编写一个函数来计算一个邮政编码到另一个邮政编码的最小距离。该函数应该获取一个邮政编码的经度和纬度,然后是一个包含邮政编码的所有经度和纬度信息的二维数组。以下是我编写的函数:

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)
我得到了价值!尝试使用此函数时出错。我检查了每个步骤,似乎是UBoundvarValues0,1导致了问题。当我尝试UBoundvarValues时,它返回0,我猜这是参数数组的第一个维度上限


我不明白为什么UBoundvarValues0,1不起作用。我认为它应该返回我的经纬度数组的最后一行号。

考虑@Mathieu Guindon的评论,并遵循以下几行:

Option Explicit

'ASSUMPTION: coordinatesArray is a 2D array with rows in dimension 1 and columns in dimension 2.
Public Function PassArray(longitude As Double, latitude As Double, coordinatesArray As Variant) As Double
    Dim rowLowerBound As Long
    Dim rowUpperBound As Long
    Dim x As Long

    'We're looking at coordinatesArray's first dimension (rows).
    'Let's consider both the lower and upper bounds, so as to adapt to the
    'configuration of coordinatesArray.
    rowLowerBound = LBound(coordinatesArray, 1)
    rowUpperBound = UBound(coordinatesArray, 1)

    'Dim arr upfront; this will be way faster than redimming within the loop.
    ReDim arr(rowLowerBound To rowUpperBound) As Double

    For x = rowLowerBound To rowUpperBound
       'Your calculations go here.
       'You can access coordinatesArray elements like so:
       'coordinatesArray(x, 1) for row x, column 1, and
       'coordinatesArray(x, 2) for row x, column 2.
       arr(x) = Sqr((longitude - coordinatesArray(x, 1)) ^ 2 + (latitude - coordinatesArray(x, 2)) ^ 2)
    Next x

    'Note that Application.WorksheetFunction.Min doesn't seem to care
    'whether arr is zero, one or n-based.
    PassArray = Application.WorksheetFunction.Min(arr)
End Function

注意,我不能保证你的距离计算;可能适用于笛卡尔坐标,但不适用于经度/纬度。

考虑@Mathieu Guindon的评论,并遵循以下思路:

Option Explicit

'ASSUMPTION: coordinatesArray is a 2D array with rows in dimension 1 and columns in dimension 2.
Public Function PassArray(longitude As Double, latitude As Double, coordinatesArray As Variant) As Double
    Dim rowLowerBound As Long
    Dim rowUpperBound As Long
    Dim x As Long

    'We're looking at coordinatesArray's first dimension (rows).
    'Let's consider both the lower and upper bounds, so as to adapt to the
    'configuration of coordinatesArray.
    rowLowerBound = LBound(coordinatesArray, 1)
    rowUpperBound = UBound(coordinatesArray, 1)

    'Dim arr upfront; this will be way faster than redimming within the loop.
    ReDim arr(rowLowerBound To rowUpperBound) As Double

    For x = rowLowerBound To rowUpperBound
       'Your calculations go here.
       'You can access coordinatesArray elements like so:
       'coordinatesArray(x, 1) for row x, column 1, and
       'coordinatesArray(x, 2) for row x, column 2.
       arr(x) = Sqr((longitude - coordinatesArray(x, 1)) ^ 2 + (latitude - coordinatesArray(x, 2)) ^ 2)
    Next x

    'Note that Application.WorksheetFunction.Min doesn't seem to care
    'whether arr is zero, one or n-based.
    PassArray = Application.WorksheetFunction.Min(arr)
End Function
注意,我不能保证你的距离计算;可能适用于笛卡尔坐标系,但不适用于经度/纬度。

我相信,只要UBoundvarValues,1就是您想要的。另外请记住,我相信ParamArray是零索引的。for循环从元素1开始,因此它将始终错过数组中的第一个元素。如果只传递一个元素,它将不会进入for循环。删除ParamArray-它没有做你认为它做的事情,并且在这个例子中只会让事情变得混乱:如果你总是只传递一个2D数组作为第三个参数,那么varValues中只有一个元素,索引为0,保留2D数组:ParamArray基本上是将2D数组嵌套到单个变量数组中,这是不需要的。删除ParamArray将简化一切,即UBoundvarValues,1而不是到处都有varValues0。还考虑取一个范围,使其更简单;然后重构到数组。我相信,只要UBoundvarValues,1就是您想要的。还要记住,我相信ParamArray是零索引的。for循环从元素1开始,因此它将始终错过数组中的第一个元素。如果只传递一个元素,它将不会进入for循环。删除ParamArray-它没有做你认为它做的事情,并且在这个例子中只会让事情变得混乱:如果你总是只传递一个2D数组作为第三个参数,那么varValues中只有一个元素,索引为0,保留2D数组:ParamArray基本上是将2D数组嵌套到单个变量数组中,这是不需要的。删除ParamArray将简化一切,即UBoundvarValues,1而不是到处都有varValues0。还考虑取一个范围,使其更简单;然后重构到数组。