我试图将一张纸上的一系列代码分解成多张。我基本上正在尝试使用" i"来循环播放多张纸。变量既可以是工作表标识符,也可以是每个工作表上有多少个代码的乘数。
有人能发现我在这里犯的错误吗?或者你建议攻击它的任何其他方式?
" BreakdownCount" =用户输入的分组大小...在这种情况下200作为测试。
谢谢!
For i = 1 To BreakdownCount
UpperRange = 8 + (i - 1) * 200
LowerRange = 8 + i * 200
MsgBox UpperRange
MsgBox LowerRange
Worksheets("Data " & i).Range(Cells(8, 2), Cells(207, 2)).Value = Worksheets("Scanner - ALL").Range(Cells(UpperRange, 2), Cells(LowerRange, 2)).Value
Next
答案 0 :(得分:1)
您需要使用其父资料表限定所有范围对象。这包括Range()中的Cells()
:
For i = 1 To BreakdownCount
UpperRange = 8 + (i - 1) * 200
LowerRange = 8 + i * 200
MsgBox UpperRange
MsgBox LowerRange
Worksheets("Data " & i).Range(Worksheets("Data " & i).Cells(8, 2), Worksheets("Data " & i).Cells(207, 2)).Value = Worksheets("Scanner - ALL").Range(Worksheets("Scanner - ALL").Cells(UpperRange, 2), Worksheets("Scanner - ALL").Cells(LowerRange, 2)).Value
Next
一个小手:
Dim wsOut as Worksheet
With Worksheets("Scanner - ALL")
For i = 1 To BreakdownCount
Set wsOut = Worksheets("Data " & i)
UpperRange = 8 + (i - 1) * 200
LowerRange = 8 + i * 200
MsgBox UpperRange
MsgBox LowerRange
wsOut.Range(wsOut.Cells(8, 2), wsOut.Cells(207, 2)).Value = .Range(.Cells(UpperRange, 2), .Cells(LowerRange, 2)).Value
Next
End With