英文:
VBA Excel - Function - length of array
问题
我发现了多次用于确定数组长度的这个函数。
对我来说它不起作用...
有人知道解决办法吗?
如果我尝试在Excel中使用这个函数,它就不起作用...
你能告诉我可能的问题是什么吗?
英文:
i found multiple times this function for determining the length of an array.
It doesn't work for me...
Anyone knows the solution?
Function GetArrayLength(arr As Variant) As Integer
If IsEmpty(arr) Then
GetArrayLength = 0
Else
GetArrayLength = UBound(arr) - LBound(arr) + 1
End If
End Function
If i try to use the function in excel then, it's not working...
enter image description here
Can you tell me what might be the problem?
答案1
得分: 3
更改您的输入参数:
Function GetArrayLength(rg As Range) As Long
Dim arr As Variant
arr = rg.Value
If IsEmpty(arr) Then
GetArrayLength = 0
ElseIf rg.Cells.Count = 1 Then
GetArrayLength = 1
Else
GetArrayLength = UBound(arr, 1) - LBound(arr, 1) + 1
End If
End Function
但您可以使用=ROWS(A1:A5)
来实现相同的结果,而无需使用UDF。
英文:
You are passing a range - which is two-dimensional array.
Change your input parameter:
Function GetArrayLength(rg As Range) As Long
Dim arr As Variant
arr = rg.Value
If IsEmpty(arr) Then
GetArrayLength = 0
ElseIf rg.Cells.Count = 1 Then
GetArrayLength = 1
Else
GetArrayLength = UBound(arr, 1) - LBound(arr, 1) + 1
End If
End Function
But you could use a =ROWS(A1:A5)
to achieve the same result without a UDF
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论