# Perseving trailing zero

#### Luin29

##### New Member
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

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``````

### Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Replies
0
Views
170
Replies
2
Views
188
Replies
3
Views
393
Replies
2
Views
192
Replies
3
Views
218

1,203,096
Messages
6,053,516
Members
444,669
Latest member
Renarian

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back