Replace Carriage Return in Text file to be imported

lman715

New Member
Joined
Sep 8, 2011
Messages
34
I am importing a text file with 5 fields. However the 5th field is an open text format and records carriage returns.

The file is comma delimited, and each new line of data will start with defined text (PD), is it possible to use the replace command to search the fifth field of each "PD" row in the text file and remove the carriage returns to have a clean file?

I found this macro and it seems to do something close, but replaces all the carriage returns in the whole file and not just the 5th field.

Here is example of what is received:
"username","access_time","dbname","object_name","command"
"PDR1","Mar 28 2011 9:56AM ","DB1","","update master set
Master= 3,
Units =6,
lug = 9"
"PDC1","Mar 28 2011 9:56AM ","DB1","","update R_New set
Master= 62,
Units= 7,
Post = 81"

This is what I need it to look like:
"username","access_time","dbname","object_name","command"
"PD9R1","Mar 28 2011 9:56AM ","DB1","","update master set Master= 3, Units =6, lug = 9"
"PD9C1","Mar 28 2011 9:56AM ","DB1","","update R_New set Master= 62, Units= 7, Post = 81"

This is the code I found, but does not completely work...
Code:
Sub line_Removal()
'
Dim strFile As String
Dim strBuffer As String
Dim ff As Integer
strFile = "C:\testing\*.log"
strBuffer = Space(FileLen(strFile))
ff = FreeFile
Open strFile For Binary Access Read As #ff
  Get #ff, , strBuffer
Close #ff
strBuffer = Replace(strBuffer, Chr(13), "")
Kill strFile
Open strFile For Binary Access Write As #ff
  Put #ff, , strBuffer
Close #ff
'
End Sub
 
This would be a case insensitive Replace

Code:
strBuffer = Replace(strBuffer, """PD", vbCrLf & """PD", [COLOR="Red"]Compare:=vbTextCompare[/COLOR])
 
Upvote 0

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Forum statistics

Threads
1,224,597
Messages
6,179,813
Members
452,945
Latest member
Bib195

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