+ Reply to Thread
Results 1 to 2 of 2

Syntax Error when copying/pasting text from excel cell to

  1. #1
    Registered User
    Join Date
    05-22-2013
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2010
    Posts
    11

    Syntax Error when copying/pasting text from excel cell to

    When I attempt to copy/paste the following text from 3 cells from an excel file:

    Total - Net Revenue
    Total - Standard Revenue
    Total - Realization Percentage

    Into the following code:

    Sub CopyColumnsToNewSheet2()
    Dim i, LastCol
    LastCol = Range("IV1").End(xlToLeft).Column
    For i = 1 To LastCol
    If UCase(Cells(1, i).Value) = "Total - Net Revenue
    " Then

    Cells(1, i).EntireColumn.Copy Destination:= _
    Sheets("Sheet2").Range("E1")
    End If
    If UCase(Cells(1, i).Value) = "Total - Standard Revenue
    " Then

    Cells(1, i).EntireColumn.Copy Destination:= _
    Sheets("Sheet2").Range("F1")
    End If
    If UCase(Cells(1, i).Value) = "Total - Realization Percentage
    " Then

    Cells(1, i).EntireColumn.Copy Destination:= _
    Sheets("Sheet2").Range("G1")
    End If
    Next
    End Sub

    1. The text in red is causing a syntax error.
    2. There appears to be a "return" entered in the code when I copy to VBA.
    3. If I manually type the text into the VB, the text is not found.


    Does anyone have any ideas on what is going on with my data. NOTE: the data was imported into excel from a CSV file. As an FYI, if I change the text to Header1, Header2 and Header3 and update the VB, the macro works (see attached screenshot oPasteSyntaxError.jpgf both sets of code).

    Thank you.
    Last edited by csmiley; 06-03-2013 at 07:39 PM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Syntax Error when copying/pasting text from excel cell to

    Two things:
    1. Remove the carriage return (as you did in #3)
    2. If you use UCase on the Cell.Value, then compare it to all upper case text.

    Try something like this...
    If UCase(Cells(1, i).Value) = "TOTAL - NET REVENUE" Then

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1