简体   繁体   English

根据公式值格式化单元格

[英]Format cell based on formula value

I have cells with data Peer(3) 我有数据Peer(3)

I get the number with VALUE(LEFT(RIGHT(F2,2))) 我得到VALUE(LEFT(RIGHT(F2,2)))的数字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. 而不是使用VBA,使用条件格式。

for eg Say your cells from F1 to F10 contain values such as Peer(2), Peer(3), Peer(1) etc 例如,假设从F1到F10的单元格包含Peer(2),Peer(3),Peer(1)等值

  1. Select the Range F1 to F10 (or to any cell which you want formatted) 选择范围F1到F10(或任何要格式化的单元格)
  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 假设活动单元格为F1,则键入公式=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. 将128设为绿色,将0设为红色和蓝色。
  7. Click "OK" till the dialog closes. 单击“确定”直到对话框关闭。

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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