I am trying to build an UDF, dealing with floating decimal points (like if input value was between 1 to 10, round value to 2 dp and if input value was between 10 to 100, round value to 1 dp, etc). And I would like to keep the trailing zeros.
I wrote the code based on these 2 threads:
How do I keep the trailing zero (or zeros) when I round with Excel VBA?
change cell format within functions in excel VBA
I know my major problem was that I am not used to UDF syntax.
Below are my lines:
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
If I change the line:
CelRef.NumberFormat = "0." & WorksheetFunction.Rept("0", DP)
to
FDP.NumberFormat = "0." & WorksheetFunction.Rept("0", DP)
the function does not work. While I keep this line, a cell with value of 3.40112458 will be converted to 3.4, not 3.40 as I expected.
How should I correct my lines so that it will work? Any help would be appreciated.
Below lines were my original version, dealing with the rounding to floating decimal points:
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