Perseving trailing zero

Luin29

New Member
Joined
Oct 7, 2015
Messages
45
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

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

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

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

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
Back
Top