Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
函数中的VBA索引_Vba_Excel - Fatal编程技术网

函数中的VBA索引

函数中的VBA索引,vba,excel,Vba,Excel,我是VBA新手(10小时),有点Python背景。在对返回函数的一部分进行索引时,是否存在与Python相当的VBA?这就是我的意思: 如果我的VBA代码如下所示: Split(Worksheets("range").Range("K2").Offset(i, 0), "-", "-1") 我只想要分割的第三部分,我怎样才能把它输出呢 我想这是一个非常简单的问题,但我似乎想不透。提前谢谢 如果此函数正在设置变量/数组变量的值,例如: Dim myArray as Variant myArray

我是VBA新手(10小时),有点Python背景。在对返回函数的一部分进行索引时,是否存在与Python相当的VBA?这就是我的意思: 如果我的VBA代码如下所示:

Split(Worksheets("range").Range("K2").Offset(i, 0), "-", "-1")
我只想要分割的第三部分,我怎样才能把它输出呢


我想这是一个非常简单的问题,但我似乎想不透。提前谢谢

如果此函数正在设置变量/数组变量的值,例如:

Dim myArray as Variant
myArray = Split(Worksheets("range").Range("K2").Offset(i, 0), "-", "-1")
然后您应该能够引用数组中的第3项,如:

Debug.Print myArray(2) 'Option Base 0 -- Default'
或者,如果您有
选项Base 1
,则:

Debug.Print myArray(3)
这些示例使用常量表达式(2或3)对数组项进行索引。您还可以使用match函数返回动态值,例如,假设您正在该数组中查找“Steve”的值:

Dim aItem as Long
aItem = Application.Match("Steve", myArray, False)
这将返回一个长/整数,您可以引用它,如下所示:

Debug.Print myArray(aItem)
干杯

类似于:

Dim splitArr() as String, Counter as Long
Dim textString as String
textString = Worksheets("range").Range("K2").Offset(i, 0).Value2


splitArr = split(textString,"-",-1)
For cntr = LBound(splitArr) To UBound(splitArr)
    'here you can process splitArr(cntr)
Next cntr

' Just accessing the third element can be done by accessing splitArr(LBound(splitArr)+2)
请注意,根据VBA中的设置数组,从0或1开始,上述代码在任何情况下都有效。还请注意,如果字符串为空,您可能需要捕获错误/最终将得到一个单位化数组。

拆分(工作表(“范围”).range(“K2”).Offset(i,0),“-”,“-1”)(2)
将使用默认的
选项Base 0
设置,但正如其他人所指出的,如果数组中没有theird元素,这可能会引发错误。