[英]Split a array in VBA and output to multiple columns in Excel
I have a long array that exceeds the maximum number of rows of a single column (1048576), and I wish to output this array into multiple columns, for example, if my array length is 3145728, and so I intend to create 3 separate arrays, each with length 1048576, so 1 to 1048576 would be output to column A, 1048577 to 2097152 to column B, and 2097153 to 3145728 to column C. My code attempted is as follows: 我的长数组超过了单列的最大行数(1048576),并且我希望将此数组输出为多列,例如,如果我的数组长度为3145728,那么我打算创建3个单独的数组,每个长度为1048576,因此1到1048576将被输出到A列,1048577到2097152会被输出到B列,2097153到3145728会被输出到C列。我尝试的代码如下:
Sub test()
'for simplicity, just created a simply long array
Dim arrIn(1 To 3145728, 1 To 1) As Long
For i = 1 To 3145728
arrIn(i, 1) = i
Next i
'created 3 separate arrays, each with length of 1048576
Dim arrOut1(1 To 1048576, 1 To 1) As Long, arrOut2(1 To 1048576, 1 To 1) As Long, arrOut3(1 To 1048576, 1 To 1) As Long
Dim p As Long, p2 As Long, p3 As Long
'because counter p is going to be from 1 to 3145728, for the second and third arrays, the counter need to restart from 1 and upto 1048576
p2 = 1
p3 = 1
For p = 1 To 3145728
Select Case p
Case Is <= 1048576
arrOut1(p, 1) = arrIn(p, 1)
Case Is <= 2097152
arrOut2(p2, 1) = arrIn(p, 1)
p2 = p2 + 1
Case Is <= 3145728
arrOut3(p3, 1) = arrIn(p, 1)
p3 = p3 + 1
End Select
Next p
Range("A1:A1048576") = arrOut1
Range("B1048577: B2097152") = arrOut2
Range("C2097153:C3145728") = arrOut3
End Sub
The first column (arrOut1) was output to column A, however, when it comes to the second column (arrOut2), VBA returns Run-time error '1004': Menthod 'Range' of object '_Global' failed. 第一列(arrOut1)输出到列A,但是,当涉及第二列(arrOut2)时,VBA返回运行时错误'1004':对象'_Global'的方法'Range'失败。
I checked the locals windows results, p2 and p3 were 1048577, and arrOut2(1,1) = 1048577, arrOut2(1,1) = 1048578, and so on, seems the arrays all get populated, however I'm not sure what is prohibiting them from being spitted out to the columns. 我检查了本地窗口的结果,p2和p3为1048577,arrOut2(1,1)= 1048577,arrOut2(1,1)= 1048578,依此类推,似乎所有数组都被填充了,但是我不确定禁止它们被吐到色谱柱上。 Thank you for your advice.
感谢您的意见。
Ok so, just realized that Range(“B1048577:B...) was nonsense... so it is solved. 好的,只要意识到Range(“ B1048577:B ...)是胡说八道...就可以解决。 Thank you!
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.