我在我的代码上得到了Next Without For编译错误,因为我有一个额外的Next语句。如果第1行中的值不是1、2或3,我希望宏移到下一列。下面是我的代码的简化版本:
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发布于 2015-10-16 20:05:43
我能够得到同样的结果,通过额外的If-然后声明:
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发布于 2015-10-16 20:55:01
这应该更快一点:
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发布于 2015-10-16 22:02:24
虽然有些人讨厌使用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 ihttps://stackoverflow.com/questions/33178724
复制相似问题