Hello John. You've helped me earlier with a number formatting macro. It works good, but I noticed that when I have numbers with more decimals ( like 0.0355) it will only leave 2 (0.03). How can I modify it to keep all its decimals? Up to 5 decimals for example
Sub FormatByDP()
Dim Cl As Range, DP As Integer, DPsep As String, n As Long
DPsep = Application.DecimalSeparator ' get local decimal sepaartor (e.g. "." or ",")
'Loop through cells in range
For Each Cl In Range("F2:M1027")
If IsNumeric(Cl.Value) And Not IsEmpty(Cl.Value) Then
n = n + 1 'count another cell
With Cl
.Value = WorksheetFunction.Round(.Value, 2)
If InStr(1, .Value, DPsep) > 0 Then ' see if there's a decimal separator
DP = Len(.Value) - InStr(1, .Value, DPsep) ' see no. decimal places remaining
Else
DP = 0
End If
Select Case DP ' pick how to format the cell based on DP
Case 0
.NumberFormat = "0"
Case 1
.NumberFormat = "0.0"
Case Else
.NumberFormat = "0.00"
End Select
End With
End If
Next Cl
MsgBox "Formatted " & n & " numbers in range"
End Sub