VBA conditional formatting

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi Guys

I need to use conditional formatting with VBA to change the colour of certain cells.

I found the code below on this site, but need to format it so it changes the color of the cell rather than the number format.

So if the word "Low" appeared in a cell, I'd want it to be red, for example - please see the draft (incomplete) code that I've written (also below). Can you use the "add color scale" method in this instance? If so, what should be typed afterwards? I've tried "vb" then the color, but that didn't work.

A complete Sub would be greatly appreciated! Thanks in advance.

Sub Macro1()

LastRw = Range("F" & Rows.Count).End(xlUp).Row

For i = 1 To LastRw
If Range("F" & i).Value = 1 Then
Range("F" & i).NumberFormat = """UP"""
Else
Range("F" & i).NumberFormat = "General"
End If
Next
End Sub
Code:
Sub CndFrm()


LastRw = Range("P" & Rows.Count).End(xlUp).Row


For i = 1 To LastRw
    If Range("P" & i).Value = "Low" Then
        Range("P" & i).FormatConditions.AddColorScale
    Else
        Range("F" & i).NumberFormat = "General"
    End If
Next
End Sub
 
Hey, you can test the colour of cells using VBAs .Interior.Color property in a MsgBox.

Using this technique I have that an orange cell has a colour number of 49407 - so try that.

Code:
Sub TestColour()
    MsgBox Selection.Interior.Color
End Sub

I used this code to get the colour of the selected cell in question (I manually applied an orange fill then tested the macro with the cell selected)

NOTE: The Color will be between 0 and 256^3 (16777216)
 
Last edited:
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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