Using VBA for Conditional Formatting in Excel 2003

wongs18

New Member
Joined
Jun 19, 2013
Messages
8
Hello everyone ! :)

I'm currently stuck because excel 2003 only has 3 criterias for conditional formatting... and I need about 22?.. :(

What I'm trying to do is I'm trying to set for example Apples to be all in colour red and Bananas to be all in Green and Strawberries to be all in Pink and Oranges to be oranges...

But I can't do that in excel 2003 as mentioned before... :confused:

Is there a way I can do this on VBA so I can set 22 criterias equal 22 different colours?

Thanks many! :)
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Sure you can!

Give this guy a go...

Code:
Sub TasteTheRainbow()


Dim rngCell As Range
Dim lngLstRow As Long
Dim strFruit(1 To 22) As String
Dim strColor(1 To 22) As Integer


lngLstRow = ActiveSheet.UsedRange.Rows.Count


strFruit(1) = "Apple"
strColor(1) = 3
strFruit(2) = "Banana"
strColor(2) = 6
strFruit(3) = "Plum"
strColor(3) = 7


For Each rngCell In Range("A1:A" & lngLstRow)
    For i = 1 To 22
        If rngCell.Value = strFruit(i) Then
            rngCell.Interior.ColorIndex = strColor(i)
        End If
    Next i
Next
       
End Sub

The only caveat is that you must use the color index for the colors.

So just to explain a little, this is using an array and will loop though each one of the values looking for the right one... you can expand this by changing a few lines...

Code:
Dim strFruit(1 To 22) As String
Dim strColor(1 To 22) As Integer

You said 22, so I set it to 22, but you can change this number to whatever you like...

Code:
strFruit(1) = "Apple"
strColor(1) = 3
Each Fruit will have a value and color index, this can be expanded as much as you need...

Code:
For Each rngCell In Range("A1:A" & lngLstRow)

I was assuming your values were in A:A, but this also can be changed to whatever your ranges needs to be!


Hope this helps, and as always be sure to back up your excel workbook before running this macro!
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,930
Members
452,367
Latest member
TePunaBloke

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