我正在尝试用Excel电子表格为我的日常工作创建一个小而有用的宏,这个宏应该根据单元格的值给出格式。
Sub decimals()
ScreenUpdating = False
If Selection.Value > 0.99999999 Then
Selection.NumberFormat = "#,#00"
Else
Selection.NumberFormat = "0.00"
End If
End Sub
代码只适用于一个单元格选择,但不适用于范围,我想它是不是缺少一些循环?打个比方?
发布于 2019-06-27 01:41:06
如果是多个单元格选择,则Selection.Value
比较将不起作用。
也许可以尝试一下这样的方法:
Sub Decimals()
If Not TypeOf Selection Is Range Then Exit Sub
Dim rng As Range
For Each rng In Selection
If IsNumeric(rng.Value) Then
If rng.Value > 0.99999999 Then
rng.NumberFormat = "#,#00"
Else
rng.NumberFormat = "0.00"
End If
End If
Next
End Sub
https://stackoverflow.com/questions/56782770
复制