VBA Format Cell Range Based

ststern45

Well-known Member
Joined
Sep 17, 2005
Messages
965
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hello everyone,

I have a set of numbers in cell range A1 through A11

I would like to format any number that matches the value in cell C1

The following code works but I wanted to ask if the code can be converted to RGB?

.ColorIndex = 6

Sub ColorCellsTest2()
Dim wS As Worksheet
Dim TdCel As Range, FCell As Range, CellVal As String
Set wS = ThisWorkbook.Sheets("Sheet1")
Set TdCel = wS.Range("A1:A11")

For Each FCell In TdCel
With FCell
CellVal = LCase(.Value)
With .Interior
Select Case True
Case CellVal = Range("C1")
.ColorIndex = 6

Case Else
.Pattern = xlNone
End Select
End With '.Interior
End With 'FCell
Next FCell
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Is there some reason you cannot just use Conditional Formatting instead?
No VBA required, and it is dynamic (automatic).
 
Upvote 0
I have additional code where I just want to format a specific range of cells. Used for back testing.
 
Upvote 0
I have additional code where I just want to format a specific range of cells.
That's fine, but there isn't any reason why you cannot use both Conditional Formatting and VBA in the same sheet.
The advantage to using Conditional Formatting is that if the data changes, you don't need to re-run your code; the cells will be updated automatically.
Also note that you can use VBA to apply the Conditional Formatting, if you like (much more efficient than using loops).
To get that code, simply turn on the VBA Recorder, and record yourself applying the Conditional Formatting manually.
 
Upvote 0

Forum statistics

Threads
1,215,642
Messages
6,125,989
Members
449,277
Latest member
Fanamos298

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