VBA Excel – 函数 – 数组长度

huangapple go评论124阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年4月17日 21:12:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035546.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定