Wrap quotes around cell values in a CSV

jae113

Board Regular
Joined
Jun 17, 2008
Messages
227
have an excel spreadsheet that I need to save as a CSV file to import into another application. For this other application, the values in the CSV file must be wrapped in quotes (which Excel does not do by default). So I need each row to look like this:

"value 1", "value 2", "value 3"

instead of

value 1, value 2, value 3
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
See if you can adapt this:

Code:
Sub Test()
'   Change to suit
    Const FileName As String = "P:\TEMP\MrExcel\TextFiles\MyFile.csv"
    Dim FileNo As Integer
    Dim Rng As Range
    Dim x As Long
    Dim y As Integer
    Dim Txt As String
    FileNo = FreeFile
    Open FileName For Output As #FileNo
    Set Rng = Worksheets("Sheet1").Range("A1").CurrentRegion
    For x = 1 To Rng.Rows.Count
        Txt = ""
        For y = 1 To Rng.Columns.Count
            If Txt = "" Then
                Txt = """" & Rng.Cells(x, y) & """"
            Else
                Txt = Txt & "," & """" & Rng.Cells(x, y) & """"
            End If
        Next y
    Print #FileNo, Txt
    Next x
    Close #FileNo
End Sub
 
Upvote 0
Open your workbook and press Alt+F11 to go to the Visual Basic Editor. Click your workbook in the Project window and choose Insert|Module from the menu. Copy the code from the Board and paste it into the window on the right. Change the FileName constant to suit. The code assumes that your data is on Sheet1 starting at A1, so you may need to adjust those references as well.

Press Alt+F11 to return to your workbook, press Alt+F8, select the macro and click Run.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top