Results 1 to 4 of 4

Thread: How to display decimal numbers in Excel sheet

  1. #1
    Join Date
    May 2008
    Posts
    979

    How to display decimal numbers in Excel sheet

    I am in big trouble with my Excel sheet. How to choose a format to two decimal for a cell that does not display "point zero zero"? I mean I want my some cell in my Excel sheet should include 2 decimal values. But every time I get a whole number.

  2. #2
    Join Date
    May 2008
    Posts
    979

    Re: How to display decimal numbers in Excel sheet

    It is certainly easier, but if you do not know the answer here is a solution to adapt:

    Code:
    Private Sub Worksheet_Change (ByVal Target As Range) 
    lim = Range("a65000" ).End(xlUp).Row
    For i = 1 To lim 
    Cells (i, 1). Select 
    Selection.NumberFormat = "0.00" 
    If Round (Selection) = Selection Then Selection.NumberFormat = "0" 
    Next i 
    End Sub
    attention, the loop is done on all the cells each time you enter data in the table. If you want to do so only for the current cell, use target:

    Code:
    Private Sub Worksheet_Change (ByVal Target As Range) 
    target.Select 
    Selection.NumberFormat = "0.00" 
    If Round (Selection) = Selection Then Selection.NumberFormat = "0" 
    
    End Sub

  3. #3
    Join Date
    May 2008
    Posts
    913

    Re: How to display decimal numbers in Excel sheet

    Gunilla solution gives the most correct (since no commas).

    Nevertheless, it would be nice to apply this function only on cells that have a certain format (style number). Otherwise, if the Excel sheet contains style data, the function will return an error quickly.

    Hence my question: how to test the formatting of a cell

    If <format then cell = numerical>
    ... as per Gunilla code
    else
    exit
    end

  4. #4
    Join Date
    May 2008
    Posts
    3,316

    Re: How to display decimal numbers in Excel sheet

    To encode this macro, in Excel (with your file open), type ALT + F11. A window will open Visual Basic. There in the window of exploration left, you have a tree. Double-click on "Sheet1", a blank window will open as a window.

    Simply code as given by Gunilla. Attention, you must either stick the first solution or the second, but not both (or comment either by adding an apostrophe before each line).

    Save and close. That is, the macro is applied to each cell that you change.
    I tested it and it works very well, except just that the macro does not distinguish between a cell-type number or another. So if you come back for example "Y" in a cell, such as the macro will run on any cell in progress, the macro will return an error every time it returns a non-digital.

    There are certainly features of types of conversions to make calculations, but I do not know them and they are certainly less "certain" that directly use the correct types.

Similar Threads

  1. Replies: 2
    Last Post: 23-02-2012, 02:59 PM
  2. How to change negative numbers to positive numbers in Excel
    By Shaina Na in forum Microsoft Project
    Replies: 3
    Last Post: 08-01-2012, 05:35 PM
  3. How to reject decimal numbers in Python?
    By The Recruiter in forum Software Development
    Replies: 5
    Last Post: 15-01-2010, 05:43 PM
  4. Replies: 2
    Last Post: 19-03-2009, 11:10 PM
  5. How to display only part of a decimal number
    By Gauresh in forum Software Development
    Replies: 2
    Last Post: 31-01-2009, 09:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,060,979.13362 seconds with 17 queries