vba colorindex if or statement not working

workindan

New Member
Joined
Jun 7, 2010
Messages
32
I'm making a basic, quick macro to add values to a cell if it's green or blue. users inputed various shades of green and blue. I pulled all the values:
'blue 33,34,37,41,8
'green 42,35,4
When I try to do an if-then statement with multiple conditions, it returns true for any color. In the example below, every Blue(41) will put a 1 in cell A, but any cell that's not Blue(41) will have a 1.

Why is this? Seemed like a simple, fast way to do this...




For i = 3 To 4000
If IsNull(Range("C" & i)) = True Then
GoTo emptycell
End If
If Range("C" & i).Interior.ColorIndex = 41 Then
Range("A" & i).Value = 1
ElseIf Range("C" & i).Interior.ColorIndex = 4 Or 35 or 42 Then
Range("A" & i).Value = 2
End If
emptycell:
Next i
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try

Code:
ElseIf Range("C" & i).Interior.ColorIndex = 4 Or Range("C" & i).Interior.ColorIndex = 35 Or Range("C" & i).Interior.ColorIndex = 42 Then

but Select Case would be better than If.
 
Upvote 0
The logic statement in your If will always be true.

Try this.
Code:
    Select Case Range("C" & i).Interior.ColorIndex
        Case 41
            Range("A" & i).Value = 1
        Case 4, 35, 42
            Range("A" & i).Value = 2
    End Select
Or this.
Code:
    If Range("C" & i).Interior.ColorIndex = 41 Then
        Range("A" & i).Value = 1
    ElseIf Range("C" & i).Interior.ColorIndex = 4 Or Range("C" & i).Interior.ColorIndex = 35 Or Range("C" & i).Interior.ColorIndex = 42 Then
        Range("A" & i).Value = 2
    End If
Both are basically the same but I like one better than the other, don't know why.:)
 
Upvote 0
I went with your case select suggestion....

Select Case Range("C" & i).Interior.ColorIndex
Case 33, 34, 37, 41, 8
Range("A" & i).Value = 1
Case 42, 35, 4
Range("A" & i).Value = 2
End Select

This worked great! Thanks for the help.
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top