如果有2个条件返回2个不同的可能值

我正在写一个代码,检查行是否为黄色,如果单元格的值为true,如果两者均为正值,则应该返回一个空白行并取消选中checkbox列表。 否则,它应该返回一个黄色的行。 我写了下面的代码,但它不工作。 我将不胜感激您的帮助

Sub desmarcar_antigos() Dim i As Integer For i = 130 To 2 Step -1 If Rows(i).EntireRow.Interior.ColorIndex = 6 Then If Cells(i, 9).Value = "TRUE" Then Rows(i).EntireRow.Interior.ColorIndex = 0 And Sheets("Planilha").CheckBox1.Value = False Else Rows(i).EntireRow.Interior.ColorIndex = 6 End If End If Next i Application.ScreenUpdating = False End Sub 

您不能使用And在一行中运行两个语句。 改变这一行:

 Rows(i).EntireRow.Interior.ColorIndex = 0 And Sheets("Planilha").CheckBox1.Value = False 

至:

 Rows(i).EntireRow.Interior.ColorIndex = 0 Sheets("Planilha").CheckBox1.Value = False 

如果你真的想在一行中运行两条语句,你可以使用: 。 例如:

 Rows(i).EntireRow.Interior.ColorIndex = 0 : Sheets("Planilha").CheckBox1.Value = False 

但应该不鼓励。

此外,你可以使用单一的If检查你的条件。 这样,如果其中一个失败,你的Else将会运行:

 If Rows(i).EntireRow.Interior.ColorIndex = 6 And Cells(i, 9).Value = "TRUE" Then Rows(i).EntireRow.Interior.ColorIndex = 0 Sheets("Planilha").CheckBox1.Value = False Else Rows(i).EntireRow.Interior.ColorIndex = 6 End If