Hello. I am trying to figure out why the code below does not produce a new row of data. For example the old.csv shows
[pre]
A B C D E
1 JOE DOE MALE UNF
2 MOE DOE MALE UF
3 FOE DOE FEMALE SPACE_HERE
4 JOE DOE SPACE_HERE CAL[/pre]
But the output to the new.csv looks like this
[pre]A B C D E F G H I J K L M N O P
1 JOE DOE MALE 2 MOE DOE MALE 3 FOE DOE FEMALE 4 JOE DOE SPACE_HERE[/pre]
The output is what I need but the writing to the new.csv is not in the original format row by row.
Please provide some help.
Sub Test()
'On Error Resume Next
Dim objFSO, dataArray, clippedArray()
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Create an array out of the CSV
'open the data file
Set oTextStream = objFSO.OpenTextFile("C:\File\Old.csv")
Set newFile = objFSO.CreateTextFile("C:\File\New.csv")
'make an array from the data file
dataArray = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close
x = 0
For Each strLine In dataArray
'Now make an array from each line
[pre] ReDim Preserve clippedArray(x)
clippedArray(x) = Split(strLine, ",")
CutColumn = 5
intCount = 0
NewLine = ""
For Each Element In clippedArray(x)
If intCount = UBound(clippedArray(x)) Then
EndChar = vbCrLf
Else
EndChar = ","
End If
If intCount <> CutColumn - 1 Then
NewLine = NewLine & Element & EndChar
End If
intCount = intCount + 1
If intCount = UBound(clippedArray(x)) + 1 Then
newFile.Write NewLine
End If
Next[/pre]
Next
End Sub
Thank You
[pre]
A B C D E
1 JOE DOE MALE UNF
2 MOE DOE MALE UF
3 FOE DOE FEMALE SPACE_HERE
4 JOE DOE SPACE_HERE CAL[/pre]
But the output to the new.csv looks like this
[pre]A B C D E F G H I J K L M N O P
1 JOE DOE MALE 2 MOE DOE MALE 3 FOE DOE FEMALE 4 JOE DOE SPACE_HERE[/pre]
The output is what I need but the writing to the new.csv is not in the original format row by row.
Please provide some help.
Sub Test()
'On Error Resume Next
Dim objFSO, dataArray, clippedArray()
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Create an array out of the CSV
'open the data file
Set oTextStream = objFSO.OpenTextFile("C:\File\Old.csv")
Set newFile = objFSO.CreateTextFile("C:\File\New.csv")
'make an array from the data file
dataArray = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close
x = 0
For Each strLine In dataArray
'Now make an array from each line
[pre] ReDim Preserve clippedArray(x)
clippedArray(x) = Split(strLine, ",")
CutColumn = 5
intCount = 0
NewLine = ""
For Each Element In clippedArray(x)
If intCount = UBound(clippedArray(x)) Then
EndChar = vbCrLf
Else
EndChar = ","
End If
If intCount <> CutColumn - 1 Then
NewLine = NewLine & Element & EndChar
End If
intCount = intCount + 1
If intCount = UBound(clippedArray(x)) + 1 Then
newFile.Write NewLine
End If
Next[/pre]
Next
End Sub
Thank You