Hi Everyone,
I have the following code that, after a series of calculations, changes the numberformat of a cell to match the original precision and coverts the cell to text in order to preserve the trailing zero. The code works up until the code changes the cell to text: [xCell.Value = WorksheetFunction.text(xCell, dict(Key))].
I imagine I am forget a step or there is some flaw in my logic and, therefore, would appreciate any help that can be provided.
Thanks
I have the following code that, after a series of calculations, changes the numberformat of a cell to match the original precision and coverts the cell to text in order to preserve the trailing zero. The code works up until the code changes the cell to text: [xCell.Value = WorksheetFunction.text(xCell, dict(Key))].
I imagine I am forget a step or there is some flaw in my logic and, therefore, would appreciate any help that can be provided.
Thanks
Code:
Dim dec As Variant, k As Variant
Dim xCell as Range, det as Range
Dim lrow as long
Dim dict As Scripting.Dictionary
Set dict = New Scripting.Dictionary
dict("0") = "0"
dict("1") = "0.0"
dict("2") = "0.00"
dict("3") = "0.000"
dict("4") = "0.0000"
dict("5") = "0.00000"
For Each xCell In Range(Cells(2, det + 1), Cells(lrow, det + 1))
If Not CStr(xCell) = xCell.Offset(, -1).Value Then
If Right(xCell.Offset(, -1), 1) = 0 And Right(xCell, 1) <> 0 And InStr(1, xCell, ".") > 0 Then
dec = Len(Mid(xCell - Int(xCell), InStr(1, xCell, "."), Len(xCell) - InStr(1, xCell, "."))) + 1
For Each Key In dict.Keys
If CStr(dec) = Key Then: xCell.Value = WorksheetFunction.text(xCell, dict(Key))
Next
ElseIf xCell.Value < 10 Then
xCell.NumberFormat = dict("1")
End If
End If
Next