Excel trailing comma bug in csv files Excel trailing comma bug in csv files vba vba

Excel trailing comma bug in csv files


Manual example;

Dim r As Range: Set r = Range("A1:C10") '// or ActiveSheet.UsedRange for everythingDim buffer As String, delimiter As String, c As RangeDim i As LongFor Each c In r    If (i Mod r.Columns.Count) = 0 Then        If (Len(buffer)) Then delimiter = vbCrLf    Else        delimiter = ","    End If    buffer = buffer & delimiter & """" & CStr(c.Value) & """" '//or c.text to preserve formatting    i = (i + 1)NextOpen "c:\xxx.csv" For Output As #1    Print #1, bufferClose #1


If you don't have commas in your data, it would be very easy to write a text reader that goes through and counts the number of columns in a line and just add commas to the end if there aren't enough. It's not ideal, but it's probably easier than writing a custom Excel to CSV converter.


Simplest way:

ActiveWorkbook.SaveAs "MyFileNameAndDir.csv", xlCSV, Local:=True