+ Reply to Thread
Results 1 to 9 of 9

Why can Excel open up properly a badly formatted csv but vba can't?

Hybrid View

  1. #1
    Registered User
    Join Date
    12-06-2006
    Location
    Vancouver
    MS-Off Ver
    2019
    Posts
    9

    Question Why can Excel open up properly a badly formatted csv but vba can't?

    Greetings,

    I am here after an afternoon of fruitless searches.
    I am hoping someone can illuminate my now zombie-level brain

    Preface: I understand as well as anyone that the csv in question shouldn't have commas as delimiters and should use a different delimiter, be it a double quote, tab, or zombie face emoji.

    My question:

    I have a 27513 lines CSV file that sometimes, rarely, has a comma where it shouldn't.
    Note: I have no control or access to the source for formatting.

    If I open the file in Excel by simply double clicking the csv file in windows explorer, Excel somehow manages to format everything nicely.
    I can see that indeed, somehow, one cell here and there does have a comma amongst its text, and yet, dear mystery, Excel manages to format it properly.

    If I use typical VBA code to import the same somewhat shoddy csv file,

    Option Explicit
    
    Private uri As String
    Private ws As Worksheet
    
    Private Sub btnUpload_Click()
    
        shtRawData.Activate
        Set ws = ActiveWorkbook.Sheets("RawData") 'set to current worksheet name
        
        uri = Application.GetOpenFilename("Text Files (*.csv),*.csv", , "Please select text file...")
        
        With ws.QueryTables.Add(Connection:="TEXT;" & uri, Destination:=ws.Range("A1"))
             .TextFileParseType = xlDelimited
             .TextFileCommaDelimiter = True
             .Refresh
        End With
    
    End Sub
    Then, the code doesn't detect and fix those rare errors.
    I understand that the code above is not broken.

    There's simply something more that Excel does when you double click on a csv file and Excel opens it up.
    And that is precisely what I can't find after an afternoon of Googling.

    What is it that Excel does to guess correctly (at least, in this case) the correct format of the file, and how can I use VBA to do the same?

    Once again, I have no control over formatting and comments to that effect are a waste of time.
    I'm hoping someone can illuminate what is Excel doing differently do fix the file when you open up the csv by double-clicking on it versus importing it in vba?


    Cheers from Vancouver,

    PS: Whoever figures this one out get a free latte card from Starbucks

  2. #2
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    935

    Re: Why can Excel open up properly a badly formatted csv but vba can't?

    What changes could be anything; could need
    .TextFileConsecutiveDelimiter = False
    Without some csv data for testing it's impossible to do analizing/debugging (just a couple of rows before and after the one that creates the problem).

  3. #3
    Registered User
    Join Date
    12-06-2006
    Location
    Vancouver
    MS-Off Ver
    2019
    Posts
    9

    Re: Why can Excel open up properly a badly formatted csv but vba can't?

    I've sanitized it and replicated the problem with just a few rows. Hey, thank you I can't upload I'm too new it seems but the t1ny u3l is slash vbacsv

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Why can Excel open up properly a badly formatted csv but vba can't?

    Opening a csv file and importing it are not the same thing at all. What happens if you use Workbooks.Open in your code instead of a query?
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Registered User
    Join Date
    12-06-2006
    Location
    Vancouver
    MS-Off Ver
    2019
    Posts
    9

    Re: Why can Excel open up properly a badly formatted csv but vba can't?

    That opens it up correctly in new Excel window. Okay, I'm getting somewhere. Now I need to google how to target the given sheet in my app instead of a separate Excel window.

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,366

    Re: Why can Excel open up properly a badly formatted csv but vba can't?

    Yes, you can!!!

    There are instructions at the top of the page explaining how to attach your sample workbook.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Why can Excel open up properly a badly formatted csv but vba can't?

    You'd have to open it as a new workbook, then copy and paste to your relevant sheet.

  8. #8
    Registered User
    Join Date
    12-06-2006
    Location
    Vancouver
    MS-Off Ver
    2019
    Posts
    9

    Re: Why can Excel open up properly a badly formatted csv but vba can't?

    Winner winner...chicken dinner PM me your email

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,971

    Re: Why can Excel open up properly a badly formatted csv but vba can't?

    Why do you need my email address?

+ 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. [SOLVED] Need to get reports from badly formatted exported data
    By TryingToLearnUK in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-06-2020, 03:19 PM
  2. [SOLVED] Date not formatted properly
    By nathan.volker12 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-16-2018, 12:28 PM
  3. Lookup from badly formatted spreadsheet
    By onkelchris in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-06-2017, 01:33 PM
  4. Replies: 2
    Last Post: 09-27-2011, 06:09 PM
  5. Can't open excel files properly
    By Edward C in forum Excel General
    Replies: 0
    Last Post: 07-08-2009, 01:50 PM
  6. [SOLVED] CSV formatted files open odly in Excel 2000
    By Janski in forum Excel General
    Replies: 3
    Last Post: 11-24-2005, 04:00 PM
  7. [SOLVED] open Excel formatted differently than the default format
    By Chip in forum Excel General
    Replies: 1
    Last Post: 11-20-2005, 06:45 PM

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