vba LBound 和 Ubound 冲突,如果数组已由 Range 分配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14033478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LBound and Ubound conflicts in case of array which has been assigned by the Range
提问by arun_roy
CODE
代码
height = objExcel1.Application.WorksheetFunction.CountA(ob3.Columns(1))
'MsgBox(height)
ReDim dataArray(height - 2, 0) ' -1 for 0 index, -1 for the first row as header row, excluded
str = ""
dataArray = ob3.Range(ob3.Cells(2, 1),ob3.Cells(height, 1)).Value
Set d = CreateObject("scripting.dictionary")
'MsgBox(LBound(DeletArr) & ":" & UBound(DeletArr))
For i = LBound(DeletArr) To UBound(DeletArr)
If Not d.exists(DeletArr(i)) Then
d(DeletArr(i)) = 0
End If
Next
MsgBox(LBound(dataArray,1) & ":" & UBound(dataArray,1))
For i = LBound(dataArray, 1) To UBound(dataArray, 1) - 1
If d.exists(dataArray(i, 1)) Then
str = str & (i+1) & ":" & (i+1) & ","
'ob3.Range(i & ":" & i).Delete
Else
'found = False
End If
Next
VBScript Array is 0 based. But why LBound(dataArray,1)
is giving starting subscript is 1
,why not 0? Ubound
is giving the number - against which i am bit confused is it the last subscript of the array or the size?
VBScript 数组是基于 0 的。但是为什么LBound(dataArray,1)
给起始下标是1
,为什么不是0?Ubound
正在给出数字 - 我有点困惑,它是数组的最后一个下标还是大小?
Thanks,
谢谢,
回答by bonCodigo
By default, the subscripts/indices of VBA arrays start at 0 (this is called the lower bound of the array) and run up to the number you specify in the Dim statement (this is called the upper bound of the array). If you would prefer your array index numbers to start at 1, include the following statement at the top of the module.
默认情况下,VBA 数组的下标/索引从 0 开始(这称为数组的下限)并运行到您在 Dim 语句中指定的数字(称为数组的上限)。如果您希望数组索引号从 1 开始,请在模块顶部包含以下语句。
Option Base 1
Option Base 1
However when an array is populated by Range
object using the Transpose
method, the array Lower bound set to 1 even you are on default Zero baed
mode. So array becomes 1 based.
但是,当Range
使用该Transpose
方法由对象填充数组时,即使您处于默认Zero baed
模式,数组下限也会设置为 1 。因此数组变为基于 1。
e.g. the following data is added using Transpose
method.
例如,使用Transpose
方法添加以下数据。
Array(1) = "Hola"
Array(2) = "Bonjour"
Array(3) = "Hello"
Array(4) = "Wei"
Good thing is that this array UBound
tells you number of elements (4) which = UBound. Unlike if it was zero based, then number of elements = Ubound + 1.
好消息是这个数组UBound
告诉你元素数 (4) = UBound。与从零开始不同,元素数 = Ubound + 1。
UBound(Array) --> 4
LBound(Array) --> 1
In current 1-based scenario, the Ubound refers to the total number of elements. So in such cases you need to amend your code to track data within the array's LBound
, UBound
properties to avoid data loss.
在当前基于 1 的场景中,Ubound 是指元素的总数。因此,在这种情况下,您需要修改代码以跟踪数组的LBound
,UBound
属性中的数据,以避免数据丢失。
And by the way, by adding Option Base 0
doesn't stop array being dimentioned to 1 based by Transpose
method.Which invalids my first comment.
顺便说一句,通过添加Option Base 0
并不会阻止数组被基于Transpose
方法的1 提及。这使我的第一条评论无效。