-2

在取消循环中有多个需要更改的变量的函数时遇到问题。我的数据存放在几行但令人讨厌的列中。我正在获取数据并将其堆叠在一起,以便对其进行更多操作。我需要一种方法来同时更改两个变量y,否则它会陷入无限循环并崩溃。nnext

我希望y's 继续正常步骤,但需要n's 与它们一起更改,否则数据只是覆盖并且它会重复自身。

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
4

2 回答 2

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
于 2016-04-07T19:00:04.227 回答
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
于 2016-04-08T07:21:43.377 回答