user7004
user7004

Reputation: 187

if with 2 conditions to return 2 different possible values

i am writing a code that checks if the row is yellow and if the value of a cell is true, if both are positive, it should return a blank row and uncheck the checkbox list. Otherwise, it should return a yellow row. I have written the following code, but it is not working. I would appreciate your help

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

Upvotes: 1

Views: 38

Answers (1)

Bond
Bond

Reputation: 16311

You can't use And to run two statements in a single line. Change this line:

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

To:

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

If you really want to run two statements on a single line, you can use :. For example:

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

but it should be discouraged.

Also, you can check both your conditions using a single If. That way, your Else will run if either fails:

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

Upvotes: 1

Related Questions