split() 后面的 (1) 是什么?

What is (1) behind the split()?

你能帮我理解下面函数末尾的 (1) 是什么吗?

Split(objWS.Cells(1, ColumnNumber).Address, "$")(1)

Split 函数 return 是一个 array,它实际上是一个值列表。从文档中查看此处:

The Split function returns a zero-based, one-dimensional array that contains a specified number of substrings.

可以使用 索引器 访问数组中的值,您可以指定要访问数组中指定位置的项目。

在您的例子中,您 return 索引 1 处的项目,这是数组中的 第二个项目,因为数组的索引从 0 开始。

实际上,如果这是长度为 6 的数组:

This
is
an
array
of
values

位置 0 将是 This,位置 1(在您的情况下)将是 is,位置 5 将是 values

顺便说一句,用更长的方式写的相当于

Dim MyArray()
Dim MyItem

' Split the address and store it in MyArray
MyArray = Split(objWS.Cells(1, ColumnNumber).Address, "$")

' Get the second item from MyArray
MyItem = MyArray(1)

在这种拆分方法中,您的地址在任何地方都用“$”拆分。

所以如果 Address 像 "Test $ New Street $ City" 它将被拆分成一个数组

[0] "Test "
[1] " New Street "
[2] " City"

使用Split(objWS.Cells(1, ColumnNumber).Address, "$")(1)将获取数组第二个位置的数据,如我示例中的" New Street "