+ Reply to Thread
Results 1 to 8 of 8

Correctly parsing a CSV file

  1. #1
    Registered User
    Join Date
    08-30-2007
    Posts
    12

    Correctly parsing a CSV file

    I have an application created on an AS400 which generates output as a Comma Delimited CSV file, which is then run through an Excel worksheet which pulls out various elements of the CSV file. The CSV file can contain over 250,000 records which makes it difficult to import the file directly, so I use this function in Excel:
    Please Login or Register  to view this content.

    The problem is with the line
    Please Login or Register  to view this content.
    , some of the data elements may contain commas inside strings and this line interprets that as a data element to be split which means that the data is then misplaced.

    Is there any VBA command available (or function) which could correctly parse a long string of text from a CSV input and ignore commas inside strings.

    To further complicate matters, numeric data which is interspersed within the data entry is simply separated by commas where as String as surrounded by quotation marks.

    The only solution I have so far is to manually modify the CSV file using Wordpad. Took me 4 hours to do this so any help appreciated.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Zaphod,

    If each line (record) that you read contains the same number of fields, and each record will always have the same field types (string, number,etc.), you could use the Input# statement. This will read each field of the record into an assigned variable. It expects the fields to separated with commas.

    Sincerely,
    Leith Ross

  3. #3
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628
    Put this function in your code:
    Please Login or Register  to view this content.
    and the change your line:
    Please Login or Register  to view this content.
    with:
    Please Login or Register  to view this content.
    You can change the char of 'myChar' but please use a char you think you will never have in your string text else the code will not work.

    This code fix only comma in string, for example: "my,text".

    I hope it can help you.

    Regards,
    Antonio

  4. #4
    Registered User
    Join Date
    08-30-2007
    Posts
    12
    Thanks Antonio

    Your code did as expected, thanks but I have found another problem.
    (trust users to come up with this one)

    How can I deal with a string like this?

    "FREIGHT TO STS, NEWPORT, UK "

    Notice there are 2 commas in the String, so while your method deals with the first one it breaks when the second one comes along.

    I need a method that can ignore ALL commas, no matter how many my users decide to pepper their strings with.

    By the way if It helps I don't have a problem with removing the Comma altogether.

  5. #5
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628
    I'm sorry... I didn't think to more than one comma.

    Please change my previous function with this:
    Please Login or Register  to view this content.
    Regards,
    Antonio

  6. #6
    Registered User
    Join Date
    08-30-2007
    Posts
    12
    Thanks Antonio


    I will download the file again and check if your correction does the trick

  7. #7
    Registered User
    Join Date
    08-30-2007
    Posts
    12

    Smile

    Thanks Antonio

    Your excellent solution sorted out my problem. Now works as it should

    Regards
    John

  8. #8
    Forum Expert
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2016
    Posts
    1,628
    Glad to know it helped you.

    Regards,
    Antonio

+ 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