Sub AddRoundFormulaWithStaticValue()
Dim selectedRange As Range
Dim numDigits As Variant
Dim cell As Range
Dim cellValue As Variant
' 通过InputBox获取用户输入的小数位数
numDigits = InputBox("请输入要保留的小数位数:", "保留小数位数", "2")
' 检查用户是否点击了取消或者输入的内容是否为空
If numDigits = "" Then Exit Sub
' 检查输入是否为有效的数字
If Not IsNumeric(numDigits) Then
MsgBox "请输入一个有效的数字。"
Exit Sub
End If
' 获取当前选中的单元格范围
Set selectedRange = Selection
' 遍历选中的单元格,并在每个单元格中添加ROUND公式
For Each cell In selectedRange
' 获取单元格的当前值
cellValue = cell.Value
' 检查单元格是否包含数值
If IsNumeric(cellValue) Then
' 将单元格的内容设置为ROUND公式,使用单元格的当前值作为参数
cell.Formula = "=ROUND(" & cellValue & "," & numDigits & ")"
End If
Next cell
End Sub