我多次阅读到代码中应避免使用GoTo,然而我经常需要创建循环,如果一个项目给我错误,代码就会停止。
在下面的情况中,我必须比较单元格的值,但如果单元格中有字符串,我会得到一个错误。
您有任何其他选项来避免下面代码中的GoTo吗?
谢谢!
Sub Conditional()
Dim x As Integer
For x = 1 To 31
On Error GoTo na
If Sheets("Sheet1").Cells(8 + x, 5) >= 0.95 Then
Sheets("Sheet2").Shapes("Shape " & x).Fill.ForeColor.RGB = RGB(0, 176, 80)
ElseIf Sheets("Sheet1").Cells(8 + x, 5) < 0.95 Then
Sheets("Sheet2").Shapes("Shape " & x).Fill.ForeColor.RGB = RGB(255, 0, 0)
End If
GoTo nextx
na:
Sheets("Sheet2").Shapes("Shape " & x).Fill.ForeColor.RGB = RGB(0, 0, 0)
On Error GoTo -1
nextx:
Next x
End Sub