Home » excel » excel – Format cell based on formula value

excel – Format cell based on formula value

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have cells with data Peer(3)

I get the number with VALUE(LEFT(RIGHT(F2,2)))

then I want to give the cell a color I am trying with but not working I get type mismatch, been trying for couple of hours and getting no where.

Thanks

Sub Format()

Dim LastRow As Long
Dim WS As Worksheet

Set WS = Sheets("sheet1")

LastRow = WS.range("F" & WS.Rows.Count).End(xlUp).Row

If WS.range("F2:F" & LastRow).Formula = "=Value(Left(Right(F2, 2)))" < 3 Then cell.Interior.ColorIndex = 10

End Sub
How to&Answers:

Perhaps

Sub Format()

Dim LastRow As Long
Dim WS As Worksheet
dim rCell as range

Set WS = Sheets("sheet1")

LastRow = WS.range("F" & WS.Rows.Count).End(xlUp).Row

for each rcell in WS.range("F2:F" & LastRow).cells
 if clng(Left(Right(rcell.value, 2), 1)) < 3 Then rcell.Interior.ColorIndex = 10
next rcell
End Sub

Answer:

Instead of using VBA, use Conditional Formatting.

for e.g.
Say your cells from F1 to F10 contain values such as Peer(2), Peer(3), Peer(1) etc

  1. Select the Range F1 to F10 (or to any cell which you want formatted)
  2. On the ribbon, click on “Conditional Formatting” -> “New Rule”
  3. Choose “Use a formula to determine which cells to format”
  4. Assuming the active cell is F1, type in the formula =VALUE(LEFT(RIGHT(F1,2))) < 3
  5. Click on “Format” button, go to “Fill” tab, click on “More Colors”, “Custom” tab.
  6. Put 128 for Green, 0 for Red and Blue.
  7. Click “OK” till the dialog closes.

Hope that helps.