简体   繁体   中英

Format cell based on formula value

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

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

Instead of using VBA, use Conditional Formatting.

for eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM