Vba UBound数组定义
理解如何使用UBound有困难 如果我有一个动态列a,其中行数不断变化,但我想在for循环中获取定义的上限-我将如何进行定义?我用下面的方法不断得到一个运行时错误Vba UBound数组定义,vba,Vba,理解如何使用UBound有困难 如果我有一个动态列a,其中行数不断变化,但我想在for循环中获取定义的上限-我将如何进行定义?我用下面的方法不断得到一个运行时错误 arr = Worksheets("Sheet1").Range("A1").CurrentRegion.Value For i = 1 to UBound(arr, 1) etc. Next i 将数组设置为范围值时,它会自动将其设置为二维数组,即使范围仅为一列。请看下面的示例: Private Sub workingWi
arr = Worksheets("Sheet1").Range("A1").CurrentRegion.Value
For i = 1 to UBound(arr, 1)
etc.
Next i
将数组设置为范围值时,它会自动将其设置为二维数组,即使范围仅为一列。请看下面的示例:
Private Sub workingWithArrayBounds()
Dim Arr As Variant
Dim RowIndex As Long
Dim ColumnIndex As Long
Arr = Worksheets("Sheet1").Range("A1").CurrentRegion.Value
'VBA FUNCTION FOR CHECKING IF SOMETHING IS AN ARRAY
If IsArray(Arr) Then
'FIRST LOOP GOES THROUGH THE ROWS
For RowIndex = LBound(Arr, 1) To UBound(Arr, 1)
'THE SECOND LOOP GOES THROUGH THE COLUMNS
For ColumnIndex = LBound(Arr, 2) To UBound(Arr, 2)
'NOW YOU HAVE ACCESS TO EACH ELEMENT USING THE ROWINDEX AND THE COLUMN INDEX
Debug.Print Arr(RowIndex, ColumnIndex)
Next
Next
End If
End Sub
如果只有一列数据,那么边界看起来就像(1到x,1到1)。所以你需要通过第二个边界。为安全起见,在阵列之间循环时,始终建议同时使用LBound
和UBound
,因为它们可能因阵列而异
另一件要检查的事情是数组是否实际分配了。在将数组设置为范围值的场景中,可以首先检查范围中是否有实际要检索的值
另一种方法是使用函数检查数组是否为空。Cpearson有一个很好的功能,以及许多其他有用的功能@
公共函数IsArrayEmpty(Arr作为变量)作为布尔值
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
“我饿了
'此函数测试数组是否为空(未分配)。返回TRUE或FALSE。
'
'VBA IsArray函数指示变量是否为数组,但它不是
'区分已分配数组和未分配数组。这两种情况都会恢复为真
'已分配和未分配的数组。此函数用于测试数组是否实际
“已经分配了。
'
'此功能实际上与IsArrayalLocation相反。
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
暗磅一样长
长得一样
呃,明白了
出错时继续下一步
如果IsArray(Arr)=False,则
'我们没有通过数组,返回True
IsArrayEmpty=True
如果结束
'尝试获取数组的UBound。如果数组是
'未分配,将发生错误。
UB=UBound(Arr,1)
如果(错误编号0),则
IsArrayEmpty=True
其他的
''''''''''''''''''''''''''''''''''''''''''
“在罕见的情况下,我
'无法可靠地答复,错误号
'对于未分配的空数组将为0。
'在这些情况下,LBound是0和
“乌邦是-1。
“为了适应这种奇怪的行为,测试
'看看LB>UB。如果是,则数组不可用
“分配。
''''''''''''''''''''''''''''''''''''''''''
呃,明白了
LB=LBound(Arr)
如果LB>UB那么
IsArrayEmpty=True
其他的
IsArrayEmpty=False
如果结束
如果结束
端函数
您提供的代码没有错误。运行时错误是什么?如果有人问你这个问题,你不想知道吗?我唯一得到运行时错误的时候是从A1开始的区域是空的或者只有一个值。在这种情况下,arr
不是一个数组,而是一个变量值。运行时错误为错误13,类型不匹配。手头的工作表在A1中确实有一个值,这是一个字符串(整个列的标题)更改为一个变量,它可以工作!非常感谢。
Public Function IsArrayEmpty(Arr As Variant) As Boolean
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' IsArrayEmpty
' This function tests whether the array is empty (unallocated). Returns TRUE or FALSE.
'
' The VBA IsArray function indicates whether a variable is an array, but it does not
' distinguish between allocated and unallocated arrays. It will return TRUE for both
' allocated and unallocated arrays. This function tests whether the array has actually
' been allocated.
'
' This function is really the reverse of IsArrayAllocated.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim LB As Long
Dim UB As Long
Err.Clear
On Error Resume Next
If IsArray(Arr) = False Then
' we weren't passed an array, return True
IsArrayEmpty = True
End If
' Attempt to get the UBound of the array. If the array is
' unallocated, an error will occur.
UB = UBound(Arr, 1)
If (Err.Number <> 0) Then
IsArrayEmpty = True
Else
''''''''''''''''''''''''''''''''''''''''''
' On rare occassion, under circumstances I
' cannot reliably replictate, Err.Number
' will be 0 for an unallocated, empty array.
' On these occassions, LBound is 0 and
' UBoung is -1.
' To accomodate the weird behavior, test to
' see if LB > UB. If so, the array is not
' allocated.
''''''''''''''''''''''''''''''''''''''''''
Err.Clear
LB = LBound(Arr)
If LB > UB Then
IsArrayEmpty = True
Else
IsArrayEmpty = False
End If
End If
End Function