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.
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.