+ Reply to Thread
Results 1 to 3 of 3

Paste two decimal number in excel without extra decimal places appearing

Hybrid View

  1. #1
    Registered User
    Join Date
    07-23-2009
    Location
    Honolulu, HI
    MS-Off Ver
    Excel 2003
    Posts
    5

    Paste two decimal number in excel without extra decimal places appearing

    I have a vba macro that takes data from one workbook and pastes it into another workbook. In doing this I have declared a few variables of type single (I only need two decimal precision). However, when I copy the values from the cells on the source workbook and paste them into the target workbook, the numbers end up having 12 decimal places. Ultimately, this extra precision causes my totals to be off by .01 or more after a while. I have tried rounding the number as I pull it off the source workbook into the variable, but that didn't matter. How do I solve this problem?

    Code for pulling data from source workbook:

        Select Case product_class
            Case 1
                lbs_of_product_cl1 = Round(lbs_of_product_cl1 + Selection.Value, 2)
                Selection.Offset(0, 4).Select
                butterfat_cl1 = Round(butterfat_cl1 + Selection.Value, 2)
            Case 2
                lbs_of_product_cl2 = Round(lbs_of_product_cl2 + Selection.Value, 2)
                Selection.Offset(0, 4).Select
                butterfat_cl2 = Round(butterfat_cl2 + Selection.Value, 2)
        End Select
    code for pasting the data into the target workbook:

    Selection.Value = lbs_of_product_cl1
    Selection.Offset(0, 1).Select
    Selection.Value = butterfat_cl1
    Selection.Offset(0, 1).Select
    Selection.Value = lbs_of_product_cl2
    Selection.Offset(0, 1).Select
    Selection.Value = butterfat_cl2
    Thanks
    Last edited by jeffery_frick; 08-13-2009 at 07:52 PM. Reason: solved

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Paste two decimal number in excel without extra decimal places appearing

    Numeric values on worksheet are always Doubles, and Singles in code will get converted to Doubles when they are saved to the sheet. The Double representation of, for example, 1.1, is not the same as the Single representation (it's a lot closer, though still not precise).

    You should declare your variables as Doubles in your code, and round as you are doing. If that doesn't solve your problem, post a workbook.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    07-23-2009
    Location
    Honolulu, HI
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Paste two decimal number in excel without extra decimal places appearing

    Thanks, worked perfect.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1