解决多个Nexts

时间:2016-04-07 17:59:18

标签: excel-vba loops for-loop mod vba

无法在循环中删除需要更改的多个变量的函数。我将数据放在几行中,但是列数很烦人。我正在获取数据并将其堆叠在一起,以便可以更多地操作它。我需要一种方法来改变变量y和... n位于同一next,否则会陷入无限循环并崩溃。

我希望y继续正常步骤,但需要n来改变它们,否则数据只是覆盖而且它会重复。

Sub pasteanswers()
Dim LastCol As Integer
Dim y As Integer
Dim x As Integer
Dim n As Integer
Dim v As Integer



With ActiveSheet
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column - 7
        r2s = (LastCol / 2)

End With

For x = 6 To 28
For n = 1 To r2s
For y = 6 To 1721

Cells(y, 8).Value = Cells(x, ((2 * n + 8))).Value
Cells(y, 9).Value = Cells(x, ((2 * n) + 9)).Value



Next y
Next n
Next x


End Sub

2 个答案:

答案 0 :(得分:0)

如果你需要n和y相等,则消除n并在其中使用y。如果没有示例数据和预期结果,很难为您测试......

Sub pasteanswers()
Dim LastCol As Integer
Dim y As Integer
Dim x As Integer
Dim v As Integer

With ActiveSheet
        LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column - 7
        r2s = (LastCol / 2)

End With

For x = 6 To 28
For y = 6 To r2s

Cells(y, 8).Value = Cells(x, ((2 * y + 8))).Value
Cells(y, 9).Value = Cells(x, ((2 * y) + 9)).Value

Next y
Next x

End Sub

答案 1 :(得分:0)

For n = 1 To r2s
    y = n + 5
    If y > 1721 Then Exit For

    Cells(y, 8).Value = Cells(x, ((2 * n + 8))).Value
    Cells(y, 9).Value = Cells(x, ((2 * n) + 9)).Value

Next n