[英]Assigning an array value to a variable in VBA causes my UDF to exit
I can't seem to figure out why this UDF is exiting on the currentInput = inputArray(i)
. 我似乎无法弄清楚为什么这个UDF在
currentInput = inputArray(i)
上退出。 Here is the relevant code: 以下是相关代码:
Function OrderRange(inputRange As Range) As Variant
Dim length As Integer
inputHeight = inputRange.Count
Dim inputArray As Variant
inputArray = inputRange
Dim strippedArray() As Variant
ReDim strippedArray(0 To (inputHeight - 1))
Dim currentInput As String
Dim i As Integer
For i = 0 To (inputHeight - 1)
currentInput = inputArray(i)
'...computations on currentInput...'
strippedArray(i) = currentInput
Next i
OrderRange = strippedArray
End Function
The debugger reaches currentInput = inputArray(i)
but once I move to the next line, the function terminates and a #VALUE!
调试器到达
currentInput = inputArray(i)
但是一旦我移至下一行,函数将终止并出现#VALUE!
error is entered into the cell I call the function from. 错误输入到我从中调用该函数的单元格中。 I know this is a specific question, but I'm sure it's a general issue and I'll edit this original post to reflect what the general problem is.
我知道这是一个特定的问题,但是我敢肯定这是一个普遍的问题,我将编辑此原始帖子以反映普遍的问题。
Edit: This is an issue regarding assignment of a range to a variant array. 编辑:这是有关将范围分配给变量数组的问题。
Variant arrays created by setting them equal to ranges have two dimensions even if they are only one column, or row, wide. 通过将它们设置为等于范围而创建的变量数组即使只有一列或一排宽,也具有两个维度。 So if you call the function with A1:A10 you'll get a 10 * 1 array.
因此,如果使用A1:A10调用该函数,则会得到10 * 1数组。 Also the lower bound of the dimensions will be one, not zero.
同样,尺寸的下限将为1,而不是零。 So you'd have to do something like:
因此,您必须执行以下操作:
For i = 1 To (inputHeight)
currentInput = inputArray(i, 1)
Also you should use Option Explicit so that you're reminded to declare all variables. 另外,您应该使用Option Explicit,以便提醒您声明所有变量。 InputHeight is never declared.
永远不会声明InputHeight。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.