Condensing my IF statement

mr.mackey

New Member
Joined
Jul 28, 2011
Messages
19
Hello, as I am new to VBA programming, I am not so good at writing concise code (obvious from what you will see below).

Can you please help me condense the following code? My range of values are C19:R19, and C55:R55.


If Range("C19") = "CAR" And (Range("C55") = "--" Or Range("C55") = "YELLOW" Or Range("C55") = "BLACK" Or Range("C55") = "RED") Then
MsgBox "The colour of your car is not valid."
Exit Sub
End If

If Range("D19") = "CAR" And (Range("D55") = "--" Or Range("D55") = "YELLOW" Or Range("D55") = "BLACK" Or Range("D55") = "RED") Then
MsgBox "The colour of your car is not valid."
Exit Sub
End If
.
.
.
.
.
.

If Range("R19") = "CAR" And (Range("R55") = "--" Or Range("R55") = "YELLOW" Or Range("R55") = "BLACK" Or Range("R55") = "RED") Then
MsgBox "The colour of your car is not valid."
Exit Sub
End If
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try

Code:
Dim C As Range
For Each C In Range("C19:R19")
    If UCase(C) = "CAR"
        Select Case UCase(Cells(55, C.Column))
            Case "--", "YELLOW", "BLACK", "RED"
                MsgBox "The colour of your car is not valid."
                Exit Sub
        End Select
    End If
Next C


Hope that helps.
 
Upvote 0
Thanks, this has been extremely helpful.

I noticed while playing around with the code, that if I use , for example "Yellow" as opposed to "YELLOW" in the code, that msgbox won't appear even if the cell is equal to Yellow.

Why does the value of yellow in the code have to be all in caps for it to work?
 
Upvote 0
Because of the Ucase(C).

That capitalizes whatever is in the Cell.
Then compares it to "YELLOW"
This makes it NOT Case Sensitive.

So regardless of the capitalization of the value in the cell, the Ucase converts it to all caps and compares it to "YELLOW"


So leave it as all caps in the code
It will not matter how it's capitalized in the cell.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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