+ Reply to Thread
Results 1 to 5 of 5

Thread: Copy cell as non-scientific number VBA

  1. #1
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2003, 2007, 2010
    Posts
    758

    Copy cell as non-scientific number VBA

    I am copying a cell from another workbook using VBA. It is importing the number with scientific notation. I want to copy the cell without the scientific notation. I have tried to format the cells in the destination worksheet prior to import and other methods but having no luck. The line I am using to copy is: (I have attached a sample spreadsheet if needed; I can't upload the data file as it is in a .csv format)
                .Cells(NextRecord2, 9).Value = "Cntrl#"
                .Cells(NextRecord2, 10).Value = Range(FieldOrder(23) & i).Value
    The entire code is:
    Sub TradeAnalysis()
    
    Dim i               As Long
    Dim j               As Long
    Dim n               As Long
    Dim LastRow         As Long
    Dim Prompt          As String
    Dim Title           As String
    Dim Path            As String
    Dim FieldOrder      As New Collection
    Dim WkbData         As Workbook
    
    Call ClearContents
    
        FieldOrder.Add "E"   '1 - Status
        FieldOrder.Add "W"   '2 - Execution Date
        FieldOrder.Add "X"   '3 - Execution Time
        FieldOrder.Add "B"   '4 - Reporting Date
        FieldOrder.Add "C"   '5 - Reporting Time
        FieldOrder.Add "H"   '6 - Symbol
        FieldOrder.Add "I"   '7 - Cusip
        FieldOrder.Add "G"   '8 - Side
        FieldOrder.Add "O"   '9 - Reporting Party Capacity
        FieldOrder.Add "J"   '10 - Quantity
        FieldOrder.Add "K"   '11 - Price
        FieldOrder.Add "AF"  '12 - Commission
        FieldOrder.Add "AV"  '13 - Yield
        FieldOrder.Add "D"   '14 - Security Type
        FieldOrder.Add "AC"  '15 - Trade Market Indicator
        FieldOrder.Add "M"   '16 - Reporting Party (RPID)
        FieldOrder.Add "N"   '17 - Reporting Party Give Up (RPGU)
        FieldOrder.Add "Q"   '18 - Contra Party (RPID)
        FieldOrder.Add "R"   '19 - Contra Party Give Up (RPGU)
        FieldOrder.Add "F"   '20 - Reversal Indictor
        FieldOrder.Add "AG"  '21 - Branch Sequence Number
        FieldOrder.Add "AR"  '22 - Client Trade ID (Reference Number)
        FieldOrder.Add "AO"  '23 - TRACE Control Number
        FieldOrder.Add "Y"   '24 - Trade Mod 1
        FieldOrder.Add "Z"   '25 - Trade Mod 2
    
        n = FieldOrder.Count
        
        Prompt = "Select the file to process."
        Path = Application.GetOpenFilename("Text Files (*.csv), *.csv", , Prompt)
        If Path = "False" Then
            GoTo ExitSub:
        End If
    
        Workbooks.OpenText Filename:=Path, Origin:=2, StartRow:=1, DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, _
            Comma:=False, Space:=False, Other:=True, OtherChar:=","  ', TrailingMinusNumbers:=True
        Set WkbData = ActiveWorkbook
        
        With wsCorporateAgency
            .Range(.Cells(2, 1), .Cells(.Rows.Count, .Columns.Count)).ClearContents
            LastRow = Range("B" & .Rows.Count).End(xlUp).Row
            NextRecord1 = 3
            NextRecord2 = 4
            
            For i = 2 To LastRow
    
                For j = 1 To 20
                    Select Case j
                        Case 1
                            .Cells(NextRecord1, j).Value = Left(Range(FieldOrder(j) & i).Value, 1)
                        Case 8
                            .Cells(NextRecord1, j).Value = Left(Range(FieldOrder(j) & i).Value, 1)
                        Case 9
                            .Cells(NextRecord1, j).Value = Left(Range(FieldOrder(j) & i).Value, 1)
                        Case 12
                            If Range(FieldOrder(j) & i).Value = "" Then
                                .Cells(NextRecord1, j).Value = 0
                            Else
                                .Cells(NextRecord1, j).Value = Range(FieldOrder(j) & i).Value
                            End If
                        Case Else
                            .Cells(NextRecord1, j).Value = Range(FieldOrder(j) & i).Value
                    End Select
                Next j
                
                .Cells(NextRecord2, 2).Value = "BR Seq#"
                .Cells(NextRecord2, 3).Value = Range(FieldOrder(21) & i).Value
    
                .Cells(NextRecord2, 5).Value = "Ref #"
                .Cells(NextRecord2, 6).Value = Range(FieldOrder(22) & i).Value
    
                .Cells(NextRecord2, 9).Value = "Cntrl#"
                .Cells(NextRecord2, 10).Value = Range(FieldOrder(23) & i).Value
    
                .Cells(NextRecord2, 13).Value = "Mod1"
                .Cells(NextRecord2, 14).Value = Range(FieldOrder(24) & i).Value
                
                .Cells(NextRecord2, 15).Value = "Mod2"
                .Cells(NextRecord2, 16).Value = Range(FieldOrder(25) & i).Value
    
                NextRecord1 = NextRecord1 + 3
                NextRecord2 = NextRecord2 + 3
                
            Next i
            WkbData.Close SaveChanges:=False
        End With
    
    ExitSub:
        
        Application.DisplayAlerts = True
        Application.EnableEvents = True
        Application.ScreenUpdating = True
        Application.StatusBar = False
        
        Set WkbData = Nothing
        
    End Sub
    Attached Files Attached Files
    Last edited by maacmaac; 02-07-2012 at 08:53 PM.

  2. #2
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: Copy cell as non-scientific number VBA

    Format the cells of interest as text beforehand (or in your code).
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,231

    Re: Copy cell as non-scientific number VBA

    In cell F4 you have 2320000000000000000 which is displayed as 2.32E+18

    The reason for that is the size of that number ... all those zeroes. Excel can only cope with 15 significant digits and here you have 20, hence the scientific notation.

    You might get away with:

                .Cells(NextRecord2, 5).Value = "Ref #"
                .Cells(NextRecord2, 6).Value = "'" & Range(FieldOrder(22) & i).Value

    To test:

    Cells(10, 5).Value = "Ref #"
    Cells(10, 6).Value = "'" & "2320000000000000000"

    Note that if I don't put the 2320000000000000000 in quotes, VBA converts it to scientific notation.


    Regards, TMS

  4. #4
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2003, 2007, 2010
    Posts
    758

    Re: Copy cell as non-scientific number VBA

    I tried following, which works
                .Cells(NextRecord2, 5).Value = "Ref #"
                .Cells(NextRecord2, 6).Value = "'" & "2320120371442520000"
    I also tried following, which did not work
                .Cells(NextRecord2, 5).Value = "Ref #"
                .Cells(NextRecord2, 6).Value = "'" & Range(FieldOrder(22) & i).Value
    I also tried to format the cells of interest first as text. That didn't work either. I have attached a copy of the source data (I converted from .csv to .xls in order to attach). The problem column is "AR". Thanks for the comments.
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2003, 2007, 2010
    Posts
    758

    Re: Copy cell as non-scientific number VBA

    I was finally able to figure out a workaround. I had to format the source file first as suggested by SHG. It probably isn't the most efficient code, but it solves what I am trying to do. Thanks for all the comments.
        Columns("AV:AV").NumberFormat = "0"
        Range("AV2").FormulaR1C1 = "=""'""&RC[-4]"
        Range("AV2").Select
        Selection.AutoFill Destination:=Range("AV2:AV" & LastRow)
        Range("AV2:AV" & LastRow).Copy
        Range("AR2").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Columns("AV:AV").Delete Shift:=xlToLeft

+ 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.2.0