how to control trailing zeroes

Plasmech

Board Regular
Joined
Oct 26, 2021
Messages
51
Office Version
  1. 365
Platform
  1. Windows
If I enter 0.020 in the formula bar, I would like Excel to show 0.020 in the cell.

If I enter 0.02 into the same cell, I would like Excel to show 0.02.

How would I go about setting the cell's custom number format?

Thank you for any replies.
 
change the cell format to Text
1739472197217.png
 
Upvote 0
Select the cells -> Ctrl + 1 -> Choose number of decimal places.
This will keep the data as number rather than text.
Screen Shot 2025-02-13 at 12.59.46 PM.png
 
Upvote 0
Select the cells -> Ctrl + 1 -> Choose number of decimal places.
This will keep the data as number rather than text.
View attachment 122345
Hmmm, I don't think that is going to help them, as the number of decimal places does not appear to be constant.
In their example, the first entry had 3 and the second had 2.
It appears that the want it to match their entry at run-time, i.e.
- if they enter 0.020, they want it to show 3 decimals
- if they enter 0.02, they want it to show 2 decimals

There is only two ways that I can think of to do what they want:
1. Change the format to TEXT, as shown in the first reply.
2. Use VBA to dynamically adjust the format of each cell as values are being entered into it.

Plasmech, if you are open to a VBA solution, please let us know.
 
Upvote 0
decimal places does not appear to be constant.
Missed that but wouldn't recommend the OP doing that as it's muddy and inconsistent. Although, I don't have the context.
 
Upvote 0
A VBA option might be a little trickier than I first thought. That is because Excel immediately drops the trailing zeroes once you enter the values.
So to try to use that value to determine the number of decimals after data entry is too late.
You would probably need something like an entry form, or entering values via VBA through input boxes.

I think the Text option recommended in the first reply is probably the best way to go.
 
Upvote 0
A VBA option might be a little trickier than I first thought. That is because Excel immediately drops the trailing zeroes once you enter the values.
So to try to use that value to determine the number of decimals after data entry is too late.
You would probably need something like an entry form, or entering values via VBA through input boxes.

I think the Text option recommended in the first reply is probably the best way to go.


It can be done in VBA, but you have to change the Target cell to Text first. The only thing I haven't been able to figure out is if you select a cell in the range but don't change anything that would leave it as text.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim d As Range, x As Integer
Set d = Intersect(Target, Range("A1:A1000"))
If d Is Nothing Then Exit Sub
If d.CountLarge > 1 Then Exit Sub
Application.EnableEvents = False
    If d <> "" Then
        x = InStr(1, d.Text, ".")
        x = Len(Mid(d.Text, x + 1))
        d = Val(d)
        d.NumberFormat = "0." & String(x, "0")
    End If
Application.EnableEvents = True
End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set d = Intersect(Target, Range("A1:A1000"))
If d Is Nothing Then Exit Sub
d.NumberFormat = "@"
End Sub
 
Upvote 0
It can be done in VBA, but you have to change the Target cell to Text first. The only thing I haven't been able to figure out is if you select a cell in the range but don't change anything that would leave it as text.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim d As Range, x As Integer
Set d = Intersect(Target, Range("A1:A1000"))
If d Is Nothing Then Exit Sub
If d.CountLarge > 1 Then Exit Sub
Application.EnableEvents = False
    If d <> "" Then
        x = InStr(1, d.Text, ".")
        x = Len(Mid(d.Text, x + 1))
        d = Val(d)
        d.NumberFormat = "0." & String(x, "0")
    End If
Application.EnableEvents = True
End Sub


Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set d = Intersect(Target, Range("A1:A1000"))
If d Is Nothing Then Exit Sub
d.NumberFormat = "@"
End Sub
Yeah, I started trying to go down track too, and things got a little messy.
When they did not respond, I did not think it was worth the effort to continue.
 
Upvote 0

Forum statistics

Threads
1,226,771
Messages
6,192,918
Members
453,766
Latest member
Gskier

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