+ Reply to Thread
Results 1 to 6 of 6

Thread: Save to a CSV with Double Quotes EXCEPT the First Column with NO Quotes

  1. #1
    Registered User
    Join Date
    05-19-2009
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    30

    Save to a CSV with Double Quotes EXCEPT the First Column with NO Quotes

    I am a little lost in making this work.

    I found a few examples around the web of exporting to CSV with Double Quotes. For example:

    http://www.markinns.com/articles/ful..._double_quotes

    http://windowssecrets.com/forums/sho...-around-values

    But I can't come up with a way to export to CSV with the first column not receiving the double quotes.

    Any thoughts?
    Last edited by justforthis1; 02-14-2012 at 08:16 PM.

  2. #2
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,385

    Re: Save to a CSV with Double Quotes EXCEPT the First Column with NO Quotes

    Provide us some sample data.
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    05-19-2009
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    30

    Re: Save to a CSV with Double Quotes EXCEPT the First Column with NO Quotes

    Hopefully these work...

    The xls is an example of the data I would be using.

    The txt is an example of the output I would be looking for.

    Thanks.
    Attached Files Attached Files

  4. #4
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,385

    Re: Save to a CSV with Double Quotes EXCEPT the First Column with NO Quotes

    I checked the output file. Why is there an extra set of double quotes at the end of each line?
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  5. #5
    Valued Forum Contributor Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds/Sheffield, England
    MS-Off Ver
    Excel 2003
    Posts
    1,031

    Re: Save to a CSV with Double Quotes EXCEPT the First Column with NO Quotes

    Maybe:
    Sub test()
    Dim arr, col1
    Dim stri$
    Dim x As Integer
    
    With Application
        arr = Sheet1.Cells(1, 1).CurrentRegion.Value
        col1 = .Transpose(.Index(arr, 0, 1))
        
        Open "D:\My Documents\Testcsv.csv" For Output As #1
            For x = 1 To UBound(arr)
                stri = Replace(Join(.Index(arr, x, 0), """,""") & """", ",""""", "")
                Print #1, col1(x) & Right(stri, Len(stri) - Len(col1(x)) - 1)
            Next x
        Close
    End With
    End Sub
    You can add the additional quotes at the end if you like
    Click the * below to say thanks

    Girls sleep with guys who use photoshop, but marry the ones who work with Excel

    Corduroy
    pillows: They're making headlines!

    Did you mean: recursion
    http://www.google.com/search?hl=en&q=recursion

  6. #6
    Registered User
    Join Date
    05-19-2009
    Location
    California
    MS-Off Ver
    Excel 2010
    Posts
    30

    Re: Save to a CSV with Double Quotes EXCEPT the First Column with NO Quotes

    Quote Originally Posted by arlu1201 View Post
    I checked the output file. Why is there an extra set of double quotes at the end of each line?
    The extra set is because that field is 99.99% of the time empty. I would like to include it if possible. Not sure if it is while also removing the blanks in the top and bottom rows?


    Quote Originally Posted by Kyle123 View Post
    Maybe:
    Sub test()
    Dim arr, col1
    Dim stri$
    Dim x As Integer
    
    With Application
        arr = Sheet1.Cells(1, 1).CurrentRegion.Value
        col1 = .Transpose(.Index(arr, 0, 1))
        
        Open "D:\My Documents\Testcsv.csv" For Output As #1
            For x = 1 To UBound(arr)
                stri = Replace(Join(.Index(arr, x, 0), """,""") & """", ",""""", "")
                Print #1, col1(x) & Right(stri, Len(stri) - Len(col1(x)) - 1)
            Next x
        Close
    End With
    End Sub
    You can add the additional quotes at the end if you like
    That seems to work. If you don't mind talking me through some of it? I don't have a lot of experience with Arrays. (though I have been reading on them quite a bit since your solution)

    arr is an array composed of all the active data right?
    col1 is a "transposed" version of the first column into a 2nd array? could you explain what the .Index(...) portion is?

    x is really just a counter that caps at the UBound of the arr array?

    stri is where i think i am having the hardest time following. Replace is finding the delimiter in the array and replacing them with """,""" etc? If you could break that whole line down for me that would be awesome...

    then we take the Col1 array which is just the first line then add back in the arr array subtracting out the length of the Col1 array correct?

    If i wanted to keep the extra double quotes at the end - would that end up in the stri line of code?
    If any of the cells are left blank it seems to shift over without leaving blank delimiters - could you explain why that is from a coding perspective? and can it be changed?


    If you don't have time to explain it to me i understand, and thanks VERY much for your help.

    If you do have time to explain then great I like to understand what I am doing and apply it to things i do in the future ... rather than just getting "free coding support" kind of thing... :D

    Thanks again for both of your time.

+ 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