CSV Export with extra spaces

bmorse

New Member
Joined
Jul 13, 2010
Messages
10
Hi,
I am exporting to a csv with commas between my fields. My data looks fine in the sheet. When I export it, it puts a space sometimes before, after, or both each comma (looking like this:
4/30/2018 ,UNALOC, 2 , 67.91 ,Aney, David, 6102 , 4 , 133.76). My export code is:

Public Sub GGExcelRowsToCSV()

Dim iPtr As String
Dim sFileName As String
Dim intFH As Integer
Dim aRange As Range
Dim iLastColumn As Integer
Dim oCell As Range
Dim iRec As Long
Dim lastRow As Long, i As Long


lastRow = Range("J" & Rows.Count).End(xlUp).Row


Set aRange = Worksheets("GG_Export").Range("C1:J" & lastRow)
Application.DefaultFilePath = "K:\Equipment\Field Repairs"
'Set aRange = Application.InputBox("Select a range:-", , Selection.Address, , , , , Type:=8)
Application.SendKeys "~"
iLastColumn = aRange.Column + aRange.Columns.Count - 1

iPtr = "GGFieldRepairs"
sFileName = "K:\Equipment\Field Repairs" & iPtr & ".csv"
sFileName = Application.GetSaveAsFilename(InitialFileName:=sFileName, FileFilter:="CSV (Comma delimited) (*.csv), *.csv")
If sFileName = "False" Then Exit Sub

Close
intFH = FreeFile()
Open sFileName For Output As intFH

iRec = 0
For Each oCell In aRange
If oCell.Column = iLastColumn Then
Print #intFH , oCell.Value
iRec = iRec + 1
Else
Print #intFH , oCell.Value; ",";
End If
Next oCell

Close intFH

MsgBox "Finished: " & CStr(iRec) & " records written to " _
& sFileName & Space(10), vbOKOnly + vbInformation

End Sub

How can I trim out those extra spaces in the csv file? Many thanks.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try using the Write # statement instead...

Code:
If oCell.Column = iLastColumn Then
    Write [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=intFH"]#intFH[/URL] , oCell.Value
    iRec = iRec + 1
Else
    Write [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=intFH"]#intFH[/URL] , oCell.Value;
End If

Hope this helps!
 
Last edited:
Upvote 0
Domenic,
That did for all fields except it put a # at the beginning and end of my date field. How can i get rid of those?
 
Upvote 0
For dates, it writes date literals to the file, signified by a # at the beginning and end of the date, as you've seen.. To write dates as formatted strings, use the Format function...

Code:
Format(oCell.Value, "mmmm d, yyyy")

Change the format as desired. So you can test for a specific column, and then use the Format function for those values in that column.
 
Last edited:
Upvote 0
That's great, I'm glad I could help!

And thanks for the feedback!

Cheers!
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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