Edit Text file

jammerdk

New Member
Joined
Feb 3, 2010
Messages
49
Hi Guys

I'm trying to edit the recent updated file in a certain directory.(Remove blanks, Remove line containing "-" in a certain position)

So fare I have this but this only remove blanks in a certain file.

Sub RemoveBlanks()

Dim Filename As String

Const ForReading = 1
Const ForWriting = 2

Filename = Worksheets("Sheet1").Range("B2").Value

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(Filename, ForReading)

Do Until objFile.AtEndOfStream
strline = objFile.Readline
strline = Trim(strline)
If Len(strline) > 0 Then
strNewContents = strNewContents & strline & vbCrLf
End If
Loop

objFile.Close

Set objFile = objFSO.OpenTextFile(Filename, ForWriting)
objFile.Write strNewContents
objFile.Close

End Sub

Hope you guys can lead me on the right track :)
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Since you're getting your file name from a cell in a sheet, have a look at the 'DIR() command, as this gets filenames from a given folder. From this, you can get each file from the folder (presumably *txt), and then put your current code inside a loop that cycles through all the files of a given type in this folder.

Code:
d = dir("c:\myfolder\*.txt")
do while d <> "" then 'd is the name of the first file found
  'do my thing here
  d = dir 'finds the next qualifying file name
loop
HTH
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

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