vba 用户自定义函数,处理小数点和尾随零
vba user defined function, dealing with decimal points and trailing zeros
我正在尝试构建一个 UDF,处理浮动小数点(例如,如果输入值介于 1 到 10 之间,则将值舍入到 2 dp,如果输入值介于 10 到 100 之间,则将值舍入到 1 dp, ETC)。我想保留尾随零。
我根据这两个线程编写了代码:
change cell format within functions in excel VBA
我知道我的主要问题是我不习惯 UDF 语法。
以下是我的台词:
Public Function FDP(Rng As Range)
Dim DP As Integer, CelRef As Range
For Each CelRef In Rng
If CelRef.Value2 >= 100 Then
DP = 0
ElseIf CelRef.Value2 < 100 And CelRef.Value2 >= 10 Then
DP = 1
ElseIf CelRef.Value2 < 10 And CelRef.Value2 >= 1 Then
DP = 2
ElseIf CelRef.Value2 < 1 And CelRef.Value2 >= 0.1 Then
DP = 3
ElseIf CelRef.Value2 < 0.1 Then
DP = 4
Else
FDP = WorksheetFunction.Round(CelRef.Value2, 3 - (Int(log(CelRef.Value2)) + 1))
End If
FDP = WorksheetFunction.Round(CelRef.Value2, DP)
CelRef.NumberFormat = "0." & WorksheetFunction.Rept("0", DP) ' not sure if this line was correct
Next CelRef
End Function
如果我换行:
CelRef.NumberFormat = "0." & WorksheetFunction.Rept("0", DP)
到
FDP.NumberFormat = "0." & WorksheetFunction.Rept("0", DP)
该功能不起作用。当我保留这条线时,值为 3.40112458 的单元格将转换为 3.4,而不是我预期的 3.40。
我应该如何更正我的台词才能让它发挥作用?任何帮助将不胜感激。
下面几行是我的原始版本,处理四舍五入到浮点数:
Public Function FDP(ByVal CelRef As Double) As Double
Dim DP As Integer
If CelRef >= 100 Then
DP = 0
ElseIf CelRef < 100 And CelRef >= 10 Then
DP = 1
ElseIf CelRef < 10 And CelRef >= 1 Then
DP = 2
ElseIf CelRef < 1 And CelRef >= 0.1 Then
DP = 3
ElseIf CelRef < 0.1 Then
DP = 4
Else
FDP = WorksheetFunction.Round(CelRef, 3 - (Int(log(CelRef)) + 1)) ' this line round to 3 significant figures
End If
FDP = WorksheetFunction.Round(CelRef, DP)
End Function
拳头小数位可以用
快速计算
DP = 3-WorksheetFunction.Floor(WorksheetFunction.Log10(Abs(x)), 1)
结果如下
' x DP
' 0.000502655 7
' 0.002513274 6
' 0.012566371 5
' 0.062831853 5
' 0.314159265 4
' 1.570796327 3
' 7.853981634 3
' 39.26990817 2
' 196.3495408 1
' 981.7477042 1
' 4908.738521 0
' 24543.69261 -1
' 122718.463 -2
然后用尾随零四舍五入,你的 只有 带有 UDF 的选项是 return 格式化字符串。
和
Function RoundCustom(ByVal x As Double) As String
Dim DP As Long, s As Long, t As String
s = CLng(Sgn(x))
x = Abs(x)
DP = 3 - CLng(WorksheetFunction.Floor(WorksheetFunction.Log10(Abs(x)), 1))
If DP >= 4 Then DP = DP - 1 ' if value <1.0 reduce the DP by one
DP = WorksheetFunction.Max(0, DP)
x = Round(x, DP)
t = Format(s*x, "0." & Strings.String(DP, "0"))
RoundCustom = t
End Function
我正在尝试构建一个 UDF,处理浮动小数点(例如,如果输入值介于 1 到 10 之间,则将值舍入到 2 dp,如果输入值介于 10 到 100 之间,则将值舍入到 1 dp, ETC)。我想保留尾随零。
我根据这两个线程编写了代码:
change cell format within functions in excel VBA
我知道我的主要问题是我不习惯 UDF 语法。
以下是我的台词:
Public Function FDP(Rng As Range)
Dim DP As Integer, CelRef As Range
For Each CelRef In Rng
If CelRef.Value2 >= 100 Then
DP = 0
ElseIf CelRef.Value2 < 100 And CelRef.Value2 >= 10 Then
DP = 1
ElseIf CelRef.Value2 < 10 And CelRef.Value2 >= 1 Then
DP = 2
ElseIf CelRef.Value2 < 1 And CelRef.Value2 >= 0.1 Then
DP = 3
ElseIf CelRef.Value2 < 0.1 Then
DP = 4
Else
FDP = WorksheetFunction.Round(CelRef.Value2, 3 - (Int(log(CelRef.Value2)) + 1))
End If
FDP = WorksheetFunction.Round(CelRef.Value2, DP)
CelRef.NumberFormat = "0." & WorksheetFunction.Rept("0", DP) ' not sure if this line was correct
Next CelRef
End Function
如果我换行:
CelRef.NumberFormat = "0." & WorksheetFunction.Rept("0", DP)
到
FDP.NumberFormat = "0." & WorksheetFunction.Rept("0", DP)
该功能不起作用。当我保留这条线时,值为 3.40112458 的单元格将转换为 3.4,而不是我预期的 3.40。
我应该如何更正我的台词才能让它发挥作用?任何帮助将不胜感激。
下面几行是我的原始版本,处理四舍五入到浮点数:
Public Function FDP(ByVal CelRef As Double) As Double
Dim DP As Integer
If CelRef >= 100 Then
DP = 0
ElseIf CelRef < 100 And CelRef >= 10 Then
DP = 1
ElseIf CelRef < 10 And CelRef >= 1 Then
DP = 2
ElseIf CelRef < 1 And CelRef >= 0.1 Then
DP = 3
ElseIf CelRef < 0.1 Then
DP = 4
Else
FDP = WorksheetFunction.Round(CelRef, 3 - (Int(log(CelRef)) + 1)) ' this line round to 3 significant figures
End If
FDP = WorksheetFunction.Round(CelRef, DP)
End Function
拳头小数位可以用
快速计算DP = 3-WorksheetFunction.Floor(WorksheetFunction.Log10(Abs(x)), 1)
结果如下
' x DP
' 0.000502655 7
' 0.002513274 6
' 0.012566371 5
' 0.062831853 5
' 0.314159265 4
' 1.570796327 3
' 7.853981634 3
' 39.26990817 2
' 196.3495408 1
' 981.7477042 1
' 4908.738521 0
' 24543.69261 -1
' 122718.463 -2
然后用尾随零四舍五入,你的 只有 带有 UDF 的选项是 return 格式化字符串。
和
Function RoundCustom(ByVal x As Double) As String
Dim DP As Long, s As Long, t As String
s = CLng(Sgn(x))
x = Abs(x)
DP = 3 - CLng(WorksheetFunction.Floor(WorksheetFunction.Log10(Abs(x)), 1))
If DP >= 4 Then DP = DP - 1 ' if value <1.0 reduce the DP by one
DP = WorksheetFunction.Max(0, DP)
x = Round(x, DP)
t = Format(s*x, "0." & Strings.String(DP, "0"))
RoundCustom = t
End Function