Hi there,
I have a macro that does a pretty good job at outputting cell values in one column to a text file, with the values delimited by a semi-colon.
However, the values all have a hard return such that each value is on another line in the txt file. How can I get it so that all values are one one line instead?
Thanks!
Here's the macro:
Sub CreateCSV()
Dim rCell As Range
Dim rRow As Range
Dim sOutput As String
Dim sFname As String, lFnum As Long
'Open a text file to write
sFname = "H:\My Documents\From Derek\Test.txt"
lFnum = FreeFile
Open sFname For Output As lFnum
'Loop through the rows'
For Each rRow In ActiveSheet.UsedRange.Rows
'Loop through the cells in the rows'
For Each rCell In rRow.Cells
If rCell.Value <> Empty Then
sOutput = sOutput & rCell.Value & ";"
End If
Next rCell
'remove the last comma'
'sOutput = Left(sOutput, Len(sOutput) - 1)
'write to the file and reinitialize the variables'
Print #lFnum, sOutput
sOutput = ""
Next rRow
'Close the file'
Close lFnum
End Sub
I have a macro that does a pretty good job at outputting cell values in one column to a text file, with the values delimited by a semi-colon.
However, the values all have a hard return such that each value is on another line in the txt file. How can I get it so that all values are one one line instead?
Thanks!
Here's the macro:
Sub CreateCSV()
Dim rCell As Range
Dim rRow As Range
Dim sOutput As String
Dim sFname As String, lFnum As Long
'Open a text file to write
sFname = "H:\My Documents\From Derek\Test.txt"
lFnum = FreeFile
Open sFname For Output As lFnum
'Loop through the rows'
For Each rRow In ActiveSheet.UsedRange.Rows
'Loop through the cells in the rows'
For Each rCell In rRow.Cells
If rCell.Value <> Empty Then
sOutput = sOutput & rCell.Value & ";"
End If
Next rCell
'remove the last comma'
'sOutput = Left(sOutput, Len(sOutput) - 1)
'write to the file and reinitialize the variables'
Print #lFnum, sOutput
sOutput = ""
Next rRow
'Close the file'
Close lFnum
End Sub