+ Reply to Thread
Results 1 to 9 of 9

Extra comma's after exporting to CSV from Excel using as my columns are inconsistent

  1. #1
    Registered User
    Join Date
    05-31-2017
    Location
    Hyderabad
    MS-Off Ver
    2007
    Posts
    5

    Extra comma's after exporting to CSV from Excel using as my columns are inconsistent

    Hi,

    I wrote an macro which creates csv file from excel. I am pasting my Data from A3 and for 1st row & 2nd row, i had hardcoded with string values as shown in macro. But the csv is having extra comma's added in the 1st row eventhough i had added only 3 columns in 1st row. Please help me to remove the extra comma's from 1st row.

    Option Explicit
    Sub ExportAsCSV()

    Dim MyFileName As String
    Dim CurrentWB As Workbook, TempWB As Workbook

    Set CurrentWB = ActiveWorkbook
    ActiveWorkbook.ActiveSheet.Range("A3:Z300").Copy

    Set TempWB = Application.Workbooks.Add(1)
    TempWB.Sheets(1).Range("A1").Value = "TQLOADLINE"
    TempWB.Sheets(1).Range("B1").Value = "TQLOADLINE"
    TempWB.Sheets(1).Range("D1").Value = "EN"
    TempWB.Sheets(1).Range("A2").Value = "TQREFNUM"
    TempWB.Sheets(1).Range("B2").Value = "TQCLASSNAME"
    TempWB.Sheets(1).Range("C2").Value = "ITEMNUM"
    TempWB.Sheets(1).Range("D2").Value = "ITEMQTY"
    TempWB.Sheets(1).Range("E2").Value = "LIKEFORLIKE"
    TempWB.Sheets(1).Range("F2").Value = "LINETYPE"
    TempWB.Sheets(1).Range("G2").Value = "TQTYPE"
    TempWB.Sheets(1).Range("H2").Value = "UNITCOST"
    TempWB.Sheets(1).Range("I2").Value = "ORDERUNIT"
    TempWB.Sheets(1).Range("J2").Value = "DESCRIPTION"
    TempWB.Sheets(1).Range("K2").Value = "DESCRIPTION_LONGDESCRIPTION"
    TempWB.Sheets(1).Range("L2").Value = "HOLDITEM"
    TempWB.Sheets(1).Range("M2").Value = "LOCATION"
    TempWB.Sheets(1).Range("N2").Value = "MANSHIPTO"
    TempWB.Sheets(1).Range("O2").Value = "REQUESTBY"
    TempWB.Sheets(1).Range("P2").Value = "REQUIREDATE"
    TempWB.Sheets(1).Range("Q2").Value = "INSPECTION"
    TempWB.Sheets(1).Range("R2").Value = "CERTIFICATION"
    TempWB.Sheets(1).Range("S2").Value = "TQDOCUMENTATION"
    TempWB.Sheets(1).Range("T2").Value = "TQTESTING"
    TempWB.Sheets(1).Range("U2").Value = "TQHIREDURATION"
    TempWB.Sheets(1).Range("V2").Value = "TQOFFHIREDATE"
    TempWB.Sheets(1).Range("W2").Value = "TQONHIREDATE"
    TempWB.Sheets(1).Range("X2").Value = "TQVENDOR"
    TempWB.Sheets(1).Range("Y2").Value = "COMMODITY"
    TempWB.Sheets(1).Range("Z2").Value = "TQGLDEBITACCT"


    TempWB.Sheets(1).Range("A3:Z300").PasteSpecial xlPasteValues



    MyFileName = CurrentWB.Path & "\" & Left(CurrentWB.Name, Len(CurrentWB.Name) - 5) & Format(Now(), "YYYY-MM-DD") & "UPload" & ".csv"

    Application.DisplayAlerts = False
    TempWB.SaveAs Filename:=MyFileName, FileFormat:=xlCSV, CreateBackup:=False, Local:=True
    TempWB.Close False
    Application.DisplayAlerts = True




    End Sub


    The Output
    TQLOADLINE,TQLOADLINE,,EN,,,,,,,,,,,,,,,,,,,,,,

    Desired output
    TQLOADLINE,TQLOADLINE,,EN

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

    Re: Extra comma's after exporting to CSV from Excel using as my columns are inconsistent

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,064

    Re: Extra comma's after exporting to CSV from Excel using as my columns are inconsistent

    A CSV file is a comma separated value file. The values are separated by commas.
    A CSV file does not hold any information that states where that data goes.
    Therefore if you omit a value (in this case C1 because you have a value for D1) a comma will be automatically added to account for the null value in C1 so the enxt piece of data EN can be placed in D1.

    If the comma wasnt there EN would appear in C1 which is not what you want therefore a comma is inserted.
    The same goes for any other blank values you have omitted.
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  4. #4
    Registered User
    Join Date
    05-31-2017
    Location
    Hyderabad
    MS-Off Ver
    2007
    Posts
    5

    Re: Extra comma's after exporting to CSV from Excel using as my columns are inconsistent

    Hi,

    I too agree with you. But my columns are not consistent.

    Row 1 will have only 4 columns
    Row 2 will have 26 ( Column Headers)
    From Row 3, the actual data will there...

  5. #5
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,064

    Re: Extra comma's after exporting to CSV from Excel using as my columns are inconsistent

    Regardless of how many columns you have the bottom line is

    if a cell has a null value and there is data in a column AFTER that null value column
    saving as a CSV file will automatically add commas to the missing values to pad out the data so the data appears in the correct column.

    Consider this CSV file:

    Please Login or Register  to view this content.
    Data will go straight into cells A1 B1 and C1.
    But if I want Bob to appear in F1 I must create a CSV file like this

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    05-31-2017
    Location
    Hyderabad
    MS-Off Ver
    2007
    Posts
    5

    Re: Extra comma's after exporting to CSV from Excel using as my columns are inconsistent

    Thank you. worked perfectly.

  7. #7
    Registered User
    Join Date
    05-31-2017
    Location
    Hyderabad
    MS-Off Ver
    2007
    Posts
    5

    Re: Extra comma's after exporting to CSV from Excel using as my columns are inconsistent

    Hi,

    Yesterday your solution worked perfectly but its adding 2 extra blank lines in the lines. So import of CSV fails in my application with these 2 blank lines. When deleted these lines using notepad works fine. can you please trim the 2 blank lines that were added in the end of the file. When opened in CSV format i can't see any difference but when opened in notepad i can see 2 blank lines were added.

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

    Re: Extra comma's after exporting to CSV from Excel using as my columns are inconsistent

    Quote Originally Posted by TN112417 View Post
    Hi,

    Yesterday your solution worked perfectly but its adding 2 extra blank lines in the lines. So import of CSV fails in my application with these 2 blank lines. When deleted these lines using notepad works fine. can you please trim the 2 blank lines that were added in the end of the file. When opened in CSV format i can't see any difference but when opened in notepad i can see 2 blank lines were added.
    You copy A3:Z300. Did the your original data only go to row 298 today?

    Can you attach an example csv file with the two blank lines?

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    Last edited by AlphaFrog; 06-01-2017 at 01:53 PM.

  9. #9
    Registered User
    Join Date
    05-31-2017
    Location
    Hyderabad
    MS-Off Ver
    2007
    Posts
    5

    Re: Extra comma's after exporting to CSV from Excel using as my columns are inconsistent

    Hi,

    Sorry for the delay in responding.. been in vacation.

    Once the csv is created by macro, i try to open it with notepad and scroll down until the last row. The Pointer goes two rows down the actual data.
    If i try to upload this, i am getting error. Then If i backspace two times and once saved i am able to upload it.

    For some reason i am not able to attach the csv file.

    C1.JPGC1.JPG
    Last edited by TN112417; 06-07-2017 at 09:35 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Remove extra comma in a text fild
    By smartcard in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-02-2014, 09:29 AM
  2. [SOLVED] exporting a column of data in excel to comma separated list in Word
    By momma_hav in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-20-2013, 04:33 PM
  3. Replies: 8
    Last Post: 08-27-2012, 04:37 PM
  4. Array and extra comma problem
    By t.jones in forum Excel General
    Replies: 6
    Last Post: 02-01-2012, 09:50 AM
  5. Replies: 0
    Last Post: 07-26-2006, 10:00 AM
  6. [SOLVED] Extra comma at the end of a function
    By SYerby in forum Excel General
    Replies: 3
    Last Post: 07-14-2006, 12:00 PM
  7. [SOLVED] ?? Extra blank lines in 'address' cell after exporting to Excel
    By Hadyn Pkok in forum Excel General
    Replies: 4
    Last Post: 04-15-2005, 07:07 PM

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