Results 1 to 5 of 5

VBA code to save only text with quote marks in a CSV

Threaded View

  1. #1
    Registered User
    Join Date
    04-29-2013
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2007
    Posts
    3

    Question VBA code to save only text with quote marks in a CSV

    Hi,

    I have a 10 column worksheet with text in the first 9 columns (A to I) and a number in column 10 (J).

    I need to save this as a CSV with quote marks around the data in the first 9 columns and without quotes around the number in column 10.

    Found the attached macro code on the MS site, however it saves the data in all columns with quote marks. Can someone help with modifying it so it will do what I require?


     Sub QuoteCommaExport()
       ' Dimension all variables.
       Dim DestFile As String
       Dim FileNum As Integer
       Dim ColumnCount As Integer
       Dim RowCount As Integer
    
       ' Prompt user for destination file name.
       DestFile = InputBox("Enter the destination filename" _
          & Chr(10) & "(with complete path):", "Quote-Comma Exporter")
    
       ' Obtain next free file handle number.
       FileNum = FreeFile()
    
       ' Turn error checking off.
       On Error Resume Next
    
       ' Attempt to open destination file for output.
       Open DestFile For Output As #FileNum
    
       ' If an error occurs report it and end.
       If Err <> 0 Then
          MsgBox "Cannot open filename " & DestFile
          End
       End If
    
       ' Turn error checking on.
       On Error GoTo 0
    
       ' Loop for each row in selection.
       For RowCount = 1 To Selection.Rows.Count
    
          ' Loop for each column in selection.
          For ColumnCount = 1 To Selection.Columns.Count
    
             ' Write current cell's text to file with quotation marks.
             Print #FileNum, """" & Selection.Cells(RowCount, _
                ColumnCount).Text & """";
    
             ' Check if cell is in last column.
             If ColumnCount = Selection.Columns.Count Then
                ' If so, then write a blank line.
                Print #FileNum,
             Else
                ' Otherwise, write a comma.
                Print #FileNum, ",";
             End If
          ' Start next iteration of ColumnCount loop.
          Next ColumnCount
       ' Start next iteration of RowCount loop.
       Next RowCount
    
       ' Close destination file.
       Close #FileNum
    End Sub
    Thanks
    Alan
    Attached Files Attached Files
    Last edited by Alanks; 07-10-2015 at 02:43 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Getting rid of double quote marks using =SUBSTITUTE function
    By Sophie.Durrant in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-25-2014, 07:13 AM
  2. Getting Rid of Extra Quote Marks in Exported Text Files
    By gabrielhuebsch in forum Excel General
    Replies: 0
    Last Post: 04-07-2010, 01:27 PM
  3. How to keep quote marks for CSV in excel
    By Matt in forum Excel General
    Replies: 0
    Last Post: 07-31-2006, 07:33 PM
  4. [SOLVED] Excel to CSV: Formatting Quote Marks
    By Stephen in forum Excel General
    Replies: 2
    Last Post: 02-15-2006, 01:20 PM
  5. [SOLVED] How do you look for quote marks?
    By ExcelMonkey in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-19-2005, 09:05 AM

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