由于额外的'下一步'

时间:2015-10-16 20:05:44

标签: excel vba excel-vba

我的代码上遇到Next Without For编译错误,因为我有一个额外的Next语句。如果第1行中的值不是123,我希望宏移至下一列。这是我的代码的简化版本:

For i = 1 To lastcol
    Select Case Sheets("Daily Report").Cells(1, i).Value
        Case 1
            sPriority = "High"
        Case 2
            sPriority = "Med"
        Case 3
            sPriority = "Low"
        Case Else
            Next i
    End Select
    Set wsTracker = Sheets(sPriority)
    'Omitted code to update data on Priority worksheet with data from Daily Report sheet.
Next i

3 个答案:

答案 0 :(得分:1)

我能够通过额外的If-Then语句获得相同的结果:

For i = 1 To lastcol
    Select Case Sheets("Daily Report").Cells(1, i).Value
        Case 1
            sPriority = "High"
        Case 2
            sPriority = "Med"
        Case 3
            sPriority = "Low"
    End Select
    If sPriority <> "" Then
        Set wsTracker = Sheets(sPriority)
        'Omitted code to update data on Priority worksheet with data from Daily Report sheet.
    End If
    sPriority = ""
Next i

答案 1 :(得分:1)

这应该快一点:

v = Array("High", "Med", "Low")
On Error Resume Next
For i = 1 To lastcol
    Err.Clear: sPriority = v(Sheets("Daily Report").Cells(1, i) - 1)
    If Err = 0 Then
        Set wsTracker = Sheets(sPriority)
        'Omitted code to update data on Priority worksheet with data from Daily Report sheet.
    End If
Next

答案 2 :(得分:0)

虽然有些人讨厌使用GoTo,但我认为在这种情况下它会很好用:

For i = 1 To lastcol
    Select Case Sheets("Daily Report").Cells(1, i).Value
        Case 1
            sPriority = "High"
        Case 2
            sPriority = "Med"
        Case 3
            sPriority = "Low"
        Case Else
            GoTo NextLoop
    End Select
    Set wsTracker = Sheets(sPriority)
    'Omitted code to update data on Priority worksheet with data from Daily Report sheet.
NextLoop:
Next i