【发布时间】:2019-03-24 01:32:27
【问题描述】:
我想知道是否可以动态调整逗号后显示的数字,例如,如果我在单元格 A1 中有数字 1.245,并且我在单元格 B1 中写入“0.01”,则单元格 A1 会自动格式化为显示“1.25”,如果我在 A1 单元格 B1 中写“0.00001”,则显示“1.24500”。
请注意,我说的不是四舍五入,而是要显示的数字。
是否可以使用公式来做到这一点? 或者至少使用 VBA?
【问题讨论】:
我想知道是否可以动态调整逗号后显示的数字,例如,如果我在单元格 A1 中有数字 1.245,并且我在单元格 B1 中写入“0.01”,则单元格 A1 会自动格式化为显示“1.25”,如果我在 A1 单元格 B1 中写“0.00001”,则显示“1.24500”。
请注意,我说的不是四舍五入,而是要显示的数字。
是否可以使用公式来做到这一点? 或者至少使用 VBA?
【问题讨论】:
你可以用 VBA 做到这一点:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim numberFormatRange As Range
Set numberFormatRange = Range("B1")
If Not Intersect(Target, numberFormatRange) Is Nothing Then
If IsNumeric(numberFormatRange) Then
Range("A1").NumberFormat = Replace$(numberFormatRange.Value, "1", "0")
End If
End If
End Sub
【讨论】: