没有下一个错误

时间:2015-03-12 16:54:29

标签: excel vba

我是编程和VBA的新手,似乎对我编写的一段代码感到有些困惑。我知道我需要在我的代码中放入两个Fors之间的下一个,我不知道应该在哪里以及应该遵循什么。

请帮忙!!如果有人可以帮助我,我的代码就在这里!

Sub Order()
    rowdata = 1
    Do While Cells(rowdata, 1) <> ""
        rowdata = rowdata + 1
    Loop    
    dataend = rowdata - 1
    rowwrite = rowdata + 2
    Cells(rowwrite, 1) = "Item Code"
    For col = 1 To 3
        Cells(rowwrite, col) = Cells(1, col)
    Next col
    rowwrite = rowwrite + 1

    target = Cells(2, 7)
    **For** rowdata = 2 To dataend
        If Cells(rowdata, 1) = target Then
            Cells(rowdata, 5) = Cells(rowdata, 5) - 1
        End If
        If Cells(rowdata, 5) = Cells(rowdata, 4) Then
            **For** col = 1 To 3
                Cells(rowwrite, col) = Cells(rowdata, col)
            Next col
            rowwrite = rowwrite + 1
        End If

End sub

提前致谢!

2 个答案:

答案 0 :(得分:0)

**For** rowdata = 2 To dataend
    If Cells(rowdata, 1) = target Then
        Cells(rowdata, 5) = Cells(rowdata, 5) - 1
    End If
    If Cells(rowdata, 5) = Cells(rowdata, 4) Then
        **For** col = 1 To 3
            Cells(rowwrite, col) = Cells(rowdata, col)
        Next col
        rowwrite = rowwrite + 1
    End If
Next rowData

答案 1 :(得分:0)

我已将您的缩进重新格式化为更标准的方法。您可以在两个地方放置“下一步”语句,这取决于您需要的逻辑。我已经介入了循环之间但是如果你可以嵌套循环(一个在另一个内部),那么这就是你需要的逻辑。

Sub Order()
    rowdata = 1
    Do While Cells(rowdata, 1) <> ""
        rowdata = rowdata + 1
    Loop
    dataend = rowdata - 1
    rowwrite = rowdata + 2
    Cells(rowwrite, 1) = "Item Code"
    For col = 1 To 3
        Cells(rowwrite, col) = Cells(1, col)
    Next col
    rowwrite = rowwrite + 1

    target = Cells(2, 7)
    For rowdata = 2 To dataend 'your ** line
        If Cells(rowdata, 1) = target Then
            Cells(rowdata, 5) = Cells(rowdata, 5) - 1
        End If
    Next 'new Next

    If Cells(rowdata, 5) = Cells(rowdata, 4) Then
        For col = 1 To 3
            Cells(rowwrite, col) = Cells(rowdata, col)
        Next col
        rowwrite = rowwrite + 1
    End If
End Sub