下一个没有 For 错误,当有一个 for 循环时



当有一个For each语句时,我在每个循环的一个循环上收到一个"下一个没有 For"。错误突出显示了 for 循环cell

'   Find and delete
Dim clear As Range
For Each clear In ActiveSheet.UsedRange
If clear.Value = "TRANS_DATE" Then clear.Cells.Delete Shift:=xlShiftUp
Next clear
Dim total As Range
For Each total In ActiveSheet.UsedRange
If total.Value = "Total Amount:" Then total.Cells.Delete Shift:=xlShiftUp
Next total
Dim cell As Range
For Each cell In ActiveSheet.UsedRange
If cell.Interior.Color = Excel.XlRgbColor.rgbYellow And cell.Font.ColorIndex = RGB(255, 0, 0) Then
cell.Delete Shift:=xlShiftUp
Next cell
Dim error As Range
For Each error In ActiveSheet.UsedRange
If error.Interior.Color = Excel.XlRgbColor.rgbYellow Then
error.EntireRow.Delete Shift:=xlShiftUp
End If
Next

任何关于可能出错的建议将不胜感激!

Dim cell As Range
For Each cell In ActiveSheet.UsedRange
If cell.Interior.Color = Excel.XlRgbColor.rgbYellow And cell.Font.ColorIndex = RGB(255, 0, 0) Then
cell.Delete Shift:=xlShiftUp
-------- need and END if here ---------
Next cell

整个代码:

Dim clear As Range
For Each clear In ActiveSheet.UsedRange
If clear.Value = "TRANS_DATE" Then clear.Cells.Delete Shift:=xlShiftUp
Next clear
Dim total As Range
For Each total In ActiveSheet.UsedRange
If total.Value = "Total Amount:" Then total.Cells.Delete Shift:=xlShiftUp
Next total
Dim cell As Range
For Each cell In ActiveSheet.UsedRange
If cell.Interior.Color = Excel.XlRgbColor.rgbYellow And cell.Font.ColorIndex = RGB(255, 0, 0) Then
cell.Delete Shift:=xlShiftUp
End If <- added this
Next cell
Dim error As Range
For Each error In ActiveSheet.UsedRange
If error.Interior.Color = Excel.XlRgbColor.rgbYellow Then
error.EntireRow.Delete Shift:=xlShiftUp
End If
Next

我总是建议对缩进有严格的规则,因为您会更容易看到这一点。If 上的新行表示您需要 End If。

相关内容

最新更新