'Next Without'因为额外的'Next'
我得到了Next Without For
编译错误,因为我有一个额外的Next
语句。 如果行1中的值不是1
或3
,我希望macros移动到下一列。 这里是我的代码的简化版本:
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
这应该快一点:
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
我能够得到一个额外的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
虽然有些人讨厌使用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