Reputation: 904
In my program, I set the background color of an excel cell using:
sheet.cells(row, column).interior.color = System.Drawing.Color.Red
In another part of my program, I want to see if the color is red, but this code:
If(sheet.cells(row, column).interior.color = System.Drawing.Color.Red) Then
'Do something
End If
It returns an 'Type cast invalid' exception.
If the color is checked by:
If(sheet.cells(row, column).style.interior.color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red))
'Do something
End If
The colors are said to be not equal (even though the cell is red), because the interior color is 16777215 and the color translator returns 255.
How can I compare the color the right way?
Upvotes: 0
Views: 2840
Reputation: 61
I'm not that sure, but System.Drawing.Color.xxx is not existing in vba. You could try this:
ActiveSheet.Cells(row, column).Interior.Color = RGB(255, 0, 0)
If (ActiveSheet.Cells(row, column).Interior.Color = RGB(255, 0, 0)) Then
'Do something
End If
You could also have a look to .Interior.ColorIndex
I think you should change the tag of question to vba instead vb.net.
Best regards.
Upvotes: 0