CSV File Help

IBAUCLAPlaya

Board Regular
Joined
Dec 17, 2007
Messages
99
I have a CSV file that is outputted from our internal system. See below for example of what it looks like when opened in notepad:
Code:
"H","A1847","11155131K","C9025","1041303334","OTHER",3,"","11/24/2009",29,"","","OH",2,350,"","N"
"D","A1847","11155131K","C9025",1,"102213",1,125,"11/24/2009","11/24/2009","","","","Property Rekey Fee","N",,"Y"
"D","A1847","11155131K","C9025",2,"102822",3,75,"11/24/2009","11/24/2009","","","","Installed Smoke Detector","N",,"Y"
"H","A1847","11155139K","C9025","1041595171","OTHER",3,"","11/24/2009",29,"","","LA",1,135,"","N"
"D","A1847","11155139K","C9025",1,"102412",1,135,"10/02/2009","11/03/2009","","","","Winterized - Dry System","N",,"Y"
"H","A1847","11155143K","C9025","1041800394","OTHER",3,"","11/24/2009",29,"","","PA",1,250,"","N"
"D","A1847","11155143K","C9025",1,"102899",1,250,"11/13/2009","11/19/2009","","","","Eviction 5 Men/2 Hours @ $25","N",,"Y"
"H","A1847","11155149K","C9025","1065015039","OTHER",3,"","11/24/2009",29,"","","LA",2,775,"","N"
"D","A1847","11155149K","C9025",1,"102811",1,500,"10/09/2009","11/04/2009","","","","Removed damaged carpets","N",,"Y"
"D","A1847","11155149K","C9025",2,"102811",1,275,"10/09/2009","11/04/2009","","","","Removed rotted flooring","N",,"Y"

When I open this in excel and modify a few cells/rows, then resave as CSV, it the quotes no longer show up around the field when opened in notepad. In order for the CSV file to function (uploading into system), it needs to have the quotes around the fields.

How do I get excel to save as a CSV and keep the quote marks around each cell?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
IBA,

Here is some code that I found (not sure if here on Mr. Excel or some other site) that will wrap all fields in double-quotes.

Jack

Code:
Sub WrapInQuotes()
    Dim TheFileSaveName As String, vFileNum As Integer, qcq As String, tempStr As String
    Dim tempStr2 As String
    Dim i As Long, j As Integer
    Dim TheFileName As String
    Dim InitFileName As String
    
         
TryAgain:
    TheFileName = ActiveWorkbook.Name
    If InStr(TheFileName, ".") = 0 Then TheFileName = TheFileName & ".csv"
    
    InitFileName = Left(TheFileName, InStr(TheFileName, ".") - 1) _
      & "_" & Format(Date, "mmddyyyy") & "_" & Format(Time, "hh-mm-ss")
    
    TheFileSaveName = Application.GetSaveAsFilename(InitialFileName:=InitFileName, _
    Filefilter:="CSV (Comma delimited) (*.csv), *.csv", _
    Title:="Please enter filename to export to")
    
    If TheFileSaveName = "False" Then End
     
    vFileNum = FreeFile()
    On Error Resume Next
    Open TheFileSaveName For Output As #vFileNum
    If Err <> 0 Then MsgBox "Cannot save to filename " & TheFileSaveName: End
    On Error GoTo 0
     
    qcq = Chr(34) & Chr(44) & Chr(34)
     
    For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row
        For j = 1 To Cells(1, Columns.Count).End(xlToLeft).Column
            tempStr2 = Cells(i, j).Text
            If j = 1 Then
                tempStr = RTrim(tempStr2)
            Else
                tempStr = tempStr & qcq & RTrim(tempStr2)
            End If
        Next j
        Print #vFileNum, Chr(34) & tempStr & Chr(34)
    Next i
     
    Close #vFileNum

End Sub
 
Upvote 0
Ok, the requirements have changed...I need to only put quotes on cells with certain criteria.

Using the original data:
If the value in column A = "H", the following columns need quotes around the cell - A-F, H-I, K-M, P-Q. The values in the rest of the columns do not need quotes. Data is only in columns A-Q.
If the value in column A = "D", the following columns need quotes around the cell - A-D, F, I-O, Q. The values in the rest of the columns do not need quotes. Data is only in columns A-Q.

Can somebody re-write the macro to do this?

Thanks!
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,167
Members
448,554
Latest member
Gleisner2

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