How do you add a rule to cells in excel?

时光毁灭记忆、已成空白 提交于 2019-12-13 03:13:16

问题


I have a set of data extracted from Hyperion Essbase in which I would like to add a rule over those cells. I've tried conditional formatting, but I'm not getting what I want.

For example some cells contain 0.000 or 13.5232 and the rule I want over them is IF(ROUND(ABS(Cell Value),2)<=0.01,0,Cell Value)

I'm not sure if my formula is the one that doesn't make sense or I'm not using conditional formatting correctly. I don't need any cells highlighted, I would just like the data in the cells rounded if they fit the criteria in my formula.


回答1:


From your question, it seems like you want to use conditional formatting to change how the numbers are formatted (for numbers whose absolute value, rounded to 2 decimal places, is <= 0.01, they should be formatted with 0 decimal places, i.e., show as 0).

This can absolutely be achieved with conditional formatting (no VBA required).

  1. Select the cells you wish to apply the conditional formatting to.
  2. Open the Conditional Formatting Rules Manager, and create a .
  3. Select Use a formula to determine which cells to format, enter the formula that identifies the cells you wish to format (make sure you eschew absolute references).
  4. Click the Format button and select Number > 0 Decimal places ()
  5. Click OK 3 times to return back to the spreadsheet, and you should see the values formatted as desired:



回答2:


If you just want to change the displayed number format then this should do.

Sub cfrZeroes()
    With Worksheets("sheet2").UsedRange.Cells
        .FormatConditions.Delete
        .FormatConditions.Add Type:=xlExpression, _
          Formula1:="=ROUND(ABS(" & .Cells(1).Address(0, 0) & "),2)<=0.01"
        .FormatConditions(.FormatConditions.Count).NumberFormat = "\0"
    End With
End Sub

That will change -0.015 to 0.015 to look like 0 but it won't change the underlying raw value.



来源:https://stackoverflow.com/questions/51065070/how-do-you-add-a-rule-to-cells-in-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!