如何解决使用
下面是一个截断的示例,用于重现错误:
问题:我应该用另一种方式声明变量吗?有更好的方法将范围(一开始始终是2D对象)转换为单个维数组吗?
我知道我可以使用循环遍历数组,并逐个保存所有2D阵列值到1D阵列。但那不够高效。想象一下非常大的工作表。
Application.Index(array, row, column)
将范围(=多维数组)转换为单维数组时遇到的每个单元格限制255个字符的问题?下面是一个截断的示例,用于重现错误:
(完整代码位于超级用户上,我试图帮助另一个用户。)错误13. 类型不匹配
如何重现
Open a new Excel sheet and insert the formula
=REPT("x",256)
to cell A1
This creates a 256 characters long string which is just 1 character too long for the last stepOpen the VBA editor (Alt+F11) and paste the below code somewhere
Execute the code line by line with F8
Function StringLengthTest() Dim arr2D As Variant Dim arr1D As Variant arr2D = Rows(1) arr1D = Application.Index(arr2D, 1, 0) End Function
You'll see the same error at the last line when Excel tries to convert a range (2D) to a 1D array while one of its cells has more than 255 characters.
=REPT("x",256)
更改为=REPT("x",255)
,然后再次运行代码。这次它将正常工作。问题:我应该用另一种方式声明变量吗?有更好的方法将范围(一开始始终是2D对象)转换为单个维数组吗?
我知道我可以使用循环遍历数组,并逐个保存所有2D阵列值到1D阵列。但那不够高效。想象一下非常大的工作表。
arr2D
作为一个二维数组来处理吗?在第一维中指定1,这样arr2D(1, v)
就等同于arr1D(v)
。 - user4039065LBound(arr2D, 2)
和UBound(arr2D, 2)
。 - user4039065Application.Transpose(Application.Transpose(Rows(1)))
=> 相同的行为...=REPT("x",256)
弹出错误 :D - Dirk ReichelJoin
...知道他会这样做 =>For Each ...
和string = string & [delimeter] & [Each]
会更快...然而...至少,这并没有回答问题... - Dirk Reichel