Excel VBA if with mutiple columns values

Apple08

Active Member
Joined
Nov 1, 2014
Messages
450
Hi All

I have a macro below to change the conditional format of the value in column B, however I need to extend it to include column E, G, H and J as well. Please could anyone help how should I update the macro below:

VBA Code:
Private Sub IconSet()

    
    Dim x As Long, lastRow As Long

    lastRow = Cells(Rows.Count, "A").End(xlUp).row
    
    For x = lastRow To 2 Step -1
        If Cells(x, 2).Value = "Green" Then
            Cells(x, 2).Value = "1"
        End If
        
        If Cells(x, 2).Value = "Amber" Then
            Cells(x, 2).Value = "2"
        End If
        
        If Cells(x, 2).Value = "Red" Then
            Cells(x, 2).Value = "3"
        End If
        
            
      
    Next x


End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
That isn't Conditional Formatting - that is direct formatting.
The negative about that is it isn't dynamic - if your data changes, the formatting won't change unless you manually run the code again.
Do you want the VBA code to actually be dynamic?

Also, are you looking at running this against existing data, or on new data as it is being entered?
 
Upvote 0
Thanks Joe4. This isn't a conditional format macro as I have another conditional format macro after I change the exist data from colour to number. Therefore I only require to have one macro to change the multiple columns RAG into numbers. this is running against existing data. Many thanks.
 
Upvote 0
OK, it is faster if we don't loop through each row one at a time (we don't need to do that, because we can replace on a whole range at once).

Try this:
VBA Code:
Private Sub IconSet()

    Dim cols()
    Dim c As Long
    Dim lr As Long
    Dim rng As Range
    
    Application.ScreenUpdating = False

'   Set columns to apply to
    cols = Array("B", "E", "G", "H", "J")
    
'   Loop through each column
    For c = LBound(cols) To UBound(cols)
'       Find last row with data in column
        lr = Cells(Rows.Count, cols(c)).End(xlUp).Row
'       Build range
        Set rng = Range(Cells(1, cols(c)), Cells(lr, cols(c)))
'       First replacement
        rng.Replace "Green", "1"
'       Second replacement
        rng.Replace "Amber", "2"
'       Third replacement
        rng.Replace "Red", "3"
    Next c

    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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