【发布时间】:2022-09-26 14:24:54
【问题描述】:
我有一些列,其中数据可以是整数或长小数(例如 0.6666667)。
我想将小数格式化为两位小数,但我不希望整数显示 .00 小数。
我在其他地方看到了一个类似的问题,这个代码作为答案,但这会在整数后留下一个小数点(5.而不是5):
Range(\"Q8:Q500\").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
\"=(Q8-INT(Q8))>0\"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.NumberFormat = \"[=0]0;.##\"
Selection.FormatConditions(1).StopIfTrue = False
我尝试了一个 if 语句来根据是否存在小数点来格式化:
For Each Cell In Columns(\"Q\")
If Cell.Value = \"*.*\" Then
Cell.NumberFormat = \"0.00\"
Else
Cell.NumberFormat = \"General\"
End If
Next
我宁愿不添加列,因为文件已经很大。
-
代替
If Cell.Value = \"*.*\" Then,与Like 运算符比较:If Cell.Value Like \"*.*\" Then -
IF Round(Cell.Value,2) <> int(cell.value) Then