Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Dec 2000
    Location
    Alberta, Canada
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Placing a SINGLE value into a cell (Excel 2000 SP1)

    When placing data into cells using VBA, I get a strange result.

    Using the code below (trimmed extensively for this example) I get the following results...
    - Place a "Single" value of "68.4" into a cell. The cell displays "68.4" but when I check, it actually contains the value "68.4000015258789"

    Can anyone tell me why?

    Sub Example()
    ' Convert a string value to a Single and place in a cell

    Dim intRow As Integer
    Dim strPart As String
    Dim sglDistance as Single
    intRow = 2
    strPart = "68.4"
    sglDistance = CSng(strPart)

    ' Single step through and
    ' sglDistance does equal "68.4"

    ' Place the data into a cell
    With ActiveSheet
    .Range("E" & intRow).Value = sglDistance
    End With

    ' Check the cell value
    ' Displays as 68.4
    ' Contains 68.4000015258789

    End Sub

    An explanation would be appreciated.

  2. #2
    Bronze Lounger
    Join Date
    Jun 2001
    Location
    New York, New York, Lebanon
    Posts
    1,449
    Thanks
    1
    Thanked 1 Time in 1 Post

    Re: Placing a SINGLE value into a cell (Excel 2000 SP1)

    Hi Fred

    Sure that is normal Excel behaviour. Excel has a 15-decimal digit precision, and if you count the numbers they will be 15.

    Try: Application.WorksheetFunction.Round(sglDistance, 1) or the VBA Round function.

    Wassim
    PS check Precison as displayed under the Tools|Options|Calculation Tab.
    <img src=/S/compute.gif border=0 alt=compute width=40 height=20> in the <img src=/S/bagged.gif border=0 alt=bagged width=22 height=22>

  3. #3
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: Placing a SINGLE value into a cell (Excel 2000 SP1)

    The same or similar phenomenon is discussed in a recent thread <A target="_blank" HREF=http://www.wopr.com/cgi-bin/w3t/showflat.pl?Cat=&Board=xl&Number=79855&page=0&view =expanded&sb=5&o=0&fpart=>Here</A>

    Andrew C

  4. #4
    New Lounger
    Join Date
    Dec 2000
    Location
    Alberta, Canada
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Placing a SINGLE value into a cell (Excel 2000 SP1)

    Thanks Andrew (and Wassim)

    As usual, a simple post to The Lounge made everything clear.

    Thanks especially for the link to the other thread... a quick and effective method of providing me more information... and very applicable too.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •