If you've ever worked with text files, you'll know how frustrating it can be to remove empty lines. Part of the problem is that you do not want to remove every blank line otherwise it might upset the formatting of the document.
This article will show you how to remove a set number of blank lines from a text file using some common sense VBA coding.
Opening And Reading The Text File
The logic of the code involves reading each line of the file, and saving the line into a text string if it meets our criteria. Then, we'll write the text string back into a new file.
First, we'll access the file system object (FSO) and open the file, which we've named "blanks.txt". The code for accessing the file system is the sort of procedure you might want to save in a code library for future reference.
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim myFile As Object
Dim filePath As String
filePath = ActiveWorkbook.path & " files blanks.txt"
Set myFile = fso.openTextFile (filePath)
Removing The Blank Lines
Now, we can read each line in the file but first we need to consider the code we need to discard the blank lines.
We'll start by defining several variables and give them initial values.
'Include the line in the new file?
Dim includeLine As Boolean
'The text string to write the included lines to
Dim allTxt as string
'The number of blank lines to include in the new file
Dim countBlanks As Long
countBlanks = 1
includeBlanks = 0
includeLine = False
The initial setting for blank lines is set at 1, so we do not upset any paragraph breaks. If we wanted to remove every blank line we'd set the variable to 0.
Now we can read the file and tell the code which lines to write to the new file.
Do Until myFile.AtEndOfStream
includeLine = False
txt = myFile.ReadLine
The default for each line is to not include it in the new file unless it meets the criteria defined in the next few lines of code. The number of blank lines are counted until a non-blank line is found and then the counter is set back to zero.
Select Case Len (txt)
If countBlanks <includeBlanks Then includeLine = True
countBlanks = countBlanks + 1
countBlanks = 0
includeLine = True
If the line meets our parameters, then we add it with a new line character to the txt string and continue the loop.
If includeLine Then
allTxt = allTxt & txt & vbCrLf
With the reading of the text file completed, we close the file and write the text string to the new file.
filePath = ActiveWorkbook.path & " files blanksRemoved.txt"
Set myFile = fso.CreateTextFile (filePath)
Set fso = Nothing
With this type of file editing, it's important not to overwrite the original file in case something unexpected occurs and you lose all the data. Even when you are sure the code is working correctly it makes sense to make a copy – using VBA – to safeguard the original data.
Because working with text files is a common occurrence for most Excel developers, it's a good idea to develop relevant procedures that you can save for future reference – instead of searching for the answer when time is at a premium.