Automatically Formatting for Decimal Places

by Allen Wyatt
(last updated March 5, 2016)

2

John has a data entry worksheet that allows users to enter information. He would like to have a cell be automatically formatted to display exactly the number of decimal places that a user types. For example, if the user types "12.345" then he would like the cell to be automatically formatted to display 3 decimal places. John knows he could use the General format for a cell (which does this nicely), but that approach doesn't work if the user enters a value that ends in 0, such as "12.34500", which he would want formatted (automatically) to display 5 decimal places.

If you are thinking that you could use a custom format to address the need, that won't work. With ever custom format we could come up with, Excel drops any trailing zeroes from what it displays. (Or, conversely, if the custom format includes "0" as a placeholder, it adds zeroes at the end of the entry.)

The easiest way to handle this, quite honestly, is to simply format the cells as Text before you start entering information. In that way, Excel will simply accept what is entered—including any trailing zeroes—and stuff it into the cell. You can, further, right-align the contents of the cells so that they at least look a bit more like numeric values.

The drawback to this is that you've got to be careful in using the values in formulas. The safest way is to simply surround any reference to the cell within the VALUE function, in this manner:

=VALUE(A1) * 1.375

Another approach is to create a macro that checks what is entered into a range of cells. Start by formatting the cells as Text, and then create a named range (DataEntry) from those cells. You can then add the following code to the code sheet for the worksheet you are using:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim c As Range
    Dim sEntry As String
    Dim dEntryNumber As Double
    Dim arr

    If Not Intersect(Target, Range("DataEntry")) Is Nothing Then
        Application.EnableEvents = False
        For Each c In Target.Cells
            If IsEmpty(c) Then
                c.NumberFormat = "@"    ' Reset to Text format
            Else
                If IsNumeric(c) Then
                    If Len(c.Value) = 0 Then
                        c.NumberFormat = "@"    ' Reset to Text format
                    Else
                        sEntry = c.Value
                        dEntryNumber = CDbl(sEntry)

                        arr = Split(sEntry, ".")
                        If UBound(arr) = 1 Then
                            ' Change NumberFormat in accordance with
                            ' the number of digits after the decimal point
                            c.NumberFormat = "0." & String(Len(arr(1)), "0")
                            c.Value = dEntryNumber
                        End If
                    End If
                End If
            End If
        Next c
        Application.EnableEvents = True
    End If
End Sub

The macro is triggered everytime something changes in the worksheet. It then checks to see if that change occurred in one of the cells in the DataEntry range. If so, then it examines what is entered in the cell (which Excel treats as text, since that's how the cell was formatted) and determines if it is a number and further how many digits there are to the right of the decimal place. It then formats the cell to have that many decimal places showing and stuffs the numeric value back into the cell.

The only condition where this approach won't work is if you place a value into a cell in the DataEntry range (which converts the cell to a numeric format) and then you enter a different numeric value in the same cell. The macro has no way of knowing, in that instance, if there are any trailing zeroes being entered. (Remember that trailing zeroes are only retained if the cell is formatted as Text. Since the cell is not, Excel lops off the trailing zeros and the macro works with that value as if it had been entered.)

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (1963) applies to Microsoft Excel 2007, 2010, 2013, and 2016.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Switching between Program Windows

It is not unusual for you to work with several (and sometimes many) programs at the same time. Each program is displayed in ...

Discover More

Options in Creating New Files

You'd think that Word would be consistent, right? Wrong! Here's one example where Word is anything but consistent when it ...

Discover More

Styles for Lines, Dashes, and Arrows

Create a simple drawing object, and Excel makes some assumptions about how that object should appear. Excel provides a wide ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

MORE EXCELTIPS (RIBBON)

Adjusting Cell Margins for More White Space

Is the information in your cells too jammed up? Here are some ways you can add some white space around that information so it ...

Discover More

Displaying Latitude and Longitude

If you work with geographic data, you may need a way to display latitude and longitude in a worksheet. This tip examines ...

Discover More

Maintaining Text Formatting in a Lookup

Want to maintain the formatting used in one cell when you use formulas to reference that text in another cell? The answer is ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments for this tip:

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 + 8?

2016-03-11 19:00:27

Peter Atherton

Mark

It is but you will lose the formatting with calculations. 10.45000 + 2 will result in 12.45 as only the significant figures are calculated.

To retain the format will need a text calc function, like this?

Function CalcTextNumb(ByVal ref As Range, _
Multiplier As Range, _
Operator As String) As String
Dim dp As Integer, dpx As Integer
Dim y As String, x As String, i As Integer

Select Case Operator
Case Is = "+"
y = ref + Multiplier
Case Is = "-"
y = ref - Multiplier
Case Is = "*"
y = ref * Multiplier
Case Is = "/"
y = ref / Multiplier

End Select

dp = (Len(ref) - 1) - Len(y) + 1
'
If Len(ref) = Len(y) Then
CalcTextNumb = y
Else: GoTo format
End If
format:
x = y
For i = 1 To dp
x = x & 0
Next
CalcTextNumb = x
End Function


2016-03-07 12:50:24

Mark Marikos

A simpler solution would be to enter the value as text (as described above) and then have a second non-entry field that calculates (converts) the entered field as a number value. then use the number value field for any calculations.


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.

Links and Sharing
Share