+ Reply to Thread
Results 1 to 6 of 6

Rounding error when copying value from one sheet to another

Hybrid View

  1. #1
    Registered User
    Join Date
    04-03-2006
    Posts
    31

    Rounding error when copying value from one sheet to another

    Hello: I am not a VBA programmer but I have gathered enough info to accomplish most of what I set out to, Much thanks to this forum. I am still having an issue with one item. When I try to copy a value from one worksheet to another it is rounding off the number to 2 decimal places rather than 4. eg shows up on sheet 3 as $13.4100 rather than $13.4135.
    Both cells are formatted to display currency to 4 decimal places.
    The value being copied from sheet one is calculated using a formula
    =IF(F31>0,M31/L31,IF(AND(G31>0,E31>0),K30,IF(AND(G31>0,E31=0),M31/L31,"")))

    and the code used to transfer the info is:
    
    Private Sub CommandButton2_Click() 'Tax Form Button - captures all sales for year
    
    Sheet3.Activate
    
    ActiveSheet.Unprotect
       
    
    Dim lastrow3 As Long
    lastrow3 = Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Row
    
    If Cells(4, "A") = "" Then
    Else
    Sheets("Sheet3").Range(Cells(4, "A"), Cells(lastrow3, "H")).Select
    Selection = ""
    
    Sheets("Sheet3").Range(Cells(lastrow3, "B"), Cells(lastrow3, "H")).Select
        
         With Selection.Borders(xlEdgeTop)
            .LineStyle = none
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = none
        End With
    End If
    
    
    
    Sheet1.Activate
    Dim i As Integer, rng As Range
    Dim lastrow As Long, nextrow As Long
    Dim Title As String
    Title = ("MY WESTJET SHARES - TAX REPORT FOR " & TextBox1.Text)
    lastrow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    nextrow = Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Row + 1
    
    Set rng = Sheets("Sheet1").Range("T4:T" & lastrow)
    
        For i = 4 To lastrow
        
                If Cells(i, "T").Value = TextBox1.Text And Cells(i, "C").Value = "Sell" Then
                
                'Range(Cells(i, "A"), Cells(i, "L")).Copy Destination:=Sheets("Sheet3").Cells(nextrow, "A")
                
                Sheets("Sheet3").Cells(nextrow, "A").Value = Range(Cells(i, "A"), Cells(i, "A")).Value 'Date
                Sheets("Sheet3").Cells(nextrow, "B").Value = Range(Cells(i, "E"), Cells(i, "E")).Value 'Share Price
                Sheets("Sheet3").Cells(nextrow, "C").Value = Range(Cells(i, "G"), Cells(i, "G")).Value '#Shares Sold
                Sheets("Sheet3").Cells(nextrow, "D").Value = Range(Cells(i, "I"), Cells(i, "I")).Value 'Price Sold For
                Sheets("Sheet3").Cells(nextrow, "E").Value = Range(Cells(i, "K"), Cells(i, "K")).Value 'ACB/Share
                Sheets("Sheet3").Cells(nextrow, "G").Value = Range(Cells(i, "J"), Cells(i, "J")).Value 'Capital Gain/Loss
                Sheets("Sheet3").Cells(nextrow, "H").Value = Range(Cells(i, "D"), Cells(i, "D")).Value 'Sales Fee
                Sheets("Sheet3").Cells(nextrow, "I").Value = Range(Cells(i, "P"), Cells(i, "P")).Value 'Date 1st
                Sheets("Sheet3").Cells(nextrow, "J").Value = Range(Cells(i, "Q"), Cells(i, "Q")).Value 'Date last
                
                'Sheets("Sheet3").Cells(nextrow, "I").Value = Range(Cells(i, "I"), Cells(i, "L")).Value
                'Sheets("Sheet3").Cells(nextrow, "J").Value = Range(Cells(i, "J"), Cells(i, "L")).Value
                'Sheets("Sheet3").Cells(nextrow, "K").Value = Range(Cells(i, "K"), Cells(i, "L")).Value
                
                'Sheets("Sheet3").Cells(nextrow, "A").Resize(1, 12).Value = Range(Cells(i, "A"), Cells(i, "L")).Value
                
               
                
                nextrow = nextrow + 1
            End If
        Next i
    
    Sheet3.Activate
    
    
        
        Dim EndRow As Long
        EndRow = Sheet3.Cells(Rows.Count, "A").End(xlUp).Row
        Cells(EndRow + 1, 1).Value = "Totals"
        
        
        Dim r As Long
    
    r = Cells(Rows.Count, "B").End(xlUp).Row
    
    Range("F2").Copy
    Range("F2").AutoFill Destination:=Range("F2", ("F4:F" & EndRow))
    
    
    Cells(3, "F").Value = "Adj. Cost Base"
        
        
        Cells(EndRow + 1, 3).FormulaR1C1 = "=SUM(R1C:R" & EndRow & "C)"
        Cells(EndRow + 1, 4).FormulaR1C1 = "=SUM(R1C:R" & EndRow & "C)"
        Cells(EndRow + 1, 6).FormulaR1C1 = "=SUM(R1C:R" & EndRow & "C)"
        Cells(EndRow + 1, 7).FormulaR1C1 = "=SUM(R1C:R" & EndRow & "C)"
        Cells(EndRow + 1, 8).FormulaR1C1 = "=SUM(R1C:R" & EndRow & "C)"
        Cells(1, 5).Value = Title
        
        'Range("A" & Rows.Count).End(xlUp).Select
        Sheets("Sheet3").Range(Cells(nextrow, "B"), Cells(nextrow, "H")).Select
        
         With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlDouble
            .ColorIndex = xlAutomatic
            .Weight = xlThick
        End With
    
    Range("A" & Rows.Count).End(xlUp).Select 'Selects the last cell in column A (used to deselect previous selection)
    'Unload Me
    
       
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    
    REPORTS.CommandButton3.Visible = True
    
    End Sub
    Sheets("Sheet3").Cells(nextrow, "E").Value = Range(Cells(i, "K"), Cells(i, "K")).Value 'ACB/Share
    This is the line giving me issues.

    On sheet 1 the value is displayed to 4 decimal points but on sheet 3 there are 4 decimal places but the last two are both zero's
    Any Ideas on why this is happening
    Last edited by xned; 02-28-2010 at 01:51 AM.

  2. #2
    Registered User
    Join Date
    04-03-2006
    Posts
    31

    Re: Rounding error when copying value from one sheet to another

    I have included a copy of the spreadsheet in case it helps. There is a button at the top of sheet one called "REPORTS" and the issue is with the tax report button on the form. Specifically the ACB / Share, column E on worksheet 3.
    Attached Files Attached Files

  3. #3
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,375

    Re: Rounding error when copying value from one sheet to another

    Holy cow! A Zip file with almost one MB?!

    Could you possibly boil it down to something more manageable? I shudder at the amount of data I'll have to wade through to get to the core of the issue. I'm sure you will be able to re-create the behaviour in a workbook of max 50Kb.

    I don't have an unlimited data plan, and I suspect most here don't either.

  4. #4
    Registered User
    Join Date
    04-03-2006
    Posts
    31

    Re: Rounding error when copying value from one sheet to another

    Will try to strip it down. This has quite a few macro's and formulas in it is why it is so large.

  5. #5
    Registered User
    Join Date
    04-03-2006
    Posts
    31

    Re: Rounding error when copying value from one sheet to another

    Here is the slimmed down version. Only 7 lines of data.
    As I mentioned above, The trouble I am having is with the "Reports" Button which opens a userform.
    This userform contains 2 buttons for preparing different reports.
    I am having trouble with both reports rounding off one of the numbers being copied from worksheet 1 to worksheet 3.
    If you type in 2005 and select either the "annual" or "Tax Report" buttons, the ACB / Share value is rounded off to 2 decimal places.
    Attached Files Attached Files
    Last edited by xned; 02-28-2010 at 04:01 AM.

  6. #6
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,375

    Re: Rounding error when copying value from one sheet to another

    Macros and formulas won't blow a file up to a size that its Zip version is 1 MB. Strip down the data. A couple of dozen lines should be sufficient!

+ 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