在脑海中运行循环:
- 当
i = 1 时,范围为"A2:H7541"(行2 到7,541)
- 当
i = 2 时,范围为"A2:H7542"(行2 到7,542)
- 当
i = 9 时,范围为"A2:H7549"(行2 到7,549)
- 当
i = 10 时,范围为"A2:H75410"(行2 到75,410)
- 当
i = 99 时,范围为"A2:H75499"(行2 到75,499)
- 当
i = 100 时,范围为"A2:H754100"(行2 到754,100)
- 当
i = 900 时,范围为"A2:H754900"(行2 到754,900)
- 当
i = 999 时,范围为"A2:H754999"(行2 到754,999)
- 当
i = 1000 时,范围为"A2:H7541000"(行2 到7,541,000)
注意,i 的每个值每经过 10 次方,行数就会增加一个数量级:
- 从
i = 9 到i = 10 你从行7,549 到75,410
- 从
i = 99 到i = 100 你从行75,499 到754,100
- 从
i = 999 到i = 1000 你从行754,100 到7,541,000
另请注意,您的目标范围行始终为 2 - 因此在每次迭代中您总是会覆盖自己。
它崩溃是因为 Excel 电子表格(自 Excel 2007 起)不能超过 1,048,576 行,因此崩溃。在 Excel 2007 之前或在现代版本的 Excel 中使用非 OOXML 电子表格时,限制为 65,355。
我预计最终会有大约1,589,583 行,但似乎只得到了大约一半。
两件事:
- Excel 不支持
1,589,583 行(如上所述,最大值为 1,048,576)。
- 按照我上面的解释,您的逻辑没有正确计算复制目标范围。
您的错误的原因是使用字符串连接(即& 运算符)而不是数字加法。
您想将A2:H754 范围内的单元格复制 2111 1930 次 - 这意味着您实际上想要这样做:
Const sourceRowLB = 2
Const sourceRowUB = 755 ' 755 not 754 because exclusive upper-bounds are easier to work with
Dim sourceRowCount = sourceRowUB - sourceRowLB
Dim lastCopyUB = 755
Dim sourceRangeExpr = GetRangeExpr( "A", sourceRowLB, "H", sourceRowUB ) ' Will be "A2:H754"
Range( sourceRangeExpr ).Copy
Const loopCount As Integer = 1389 ' This cannot be 2111 because ( 2111 * 754 ) exceeds the maximum row count
For i = 1 ToloopCount ' Loop 1389 times
' Recompute the destination range:
Dim destRowLB As Integer
destRowLB = lastCopyUB
Dim destRowUB As Integer
destRowUB = destRowLB + sourceRowCount
Dim rangeExpression As String
rangeExpression = GetRangeExpr( "A", destRowLB, "H" & destRowUB )
Range( rangeExpression ).PasteSpecial Paste:=xlPasteValues
lastCopyUB = destRowUB
Next i
Function GetRangeExpr(startCol As String, startRow As Integer, endCol As String, endRowExclUB As Integer) As String
GetRangeExpr = startCol & CStr( destRowLB ) & ":" & endCol & CStr( endRowExclUB - 1 ) ' We convert endRowExclUB to an inclusive upper-bound here
End Function