Categories: News

How To Use VBA Code To Remove Blank Lines From A Text File

[ad_1]

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 don’t 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 & "filesblanks.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 don’t 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)

Case 0

If countBlanks < includeBlanks Then includeLine = True

countBlanks = countBlanks + 1

Case Else

countBlanks = 0

includeLine = True

End Select

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
End If
Loop

With the reading of the text file completed, we close the file and write the text string to the new file.

myFile.close

filePath = ActiveWorkbook.path & "filesblanksRemoved.txt"
Set myFile = fso.CreateTextFile(filePath)
myFile.Write allTxt
myFile.Close
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.

Summary

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.

[ad_2]

techfeatured

Recent Posts

Why Document Shredding is Essential for Businesses

Key Takeaways Understand the importance of document shredding for data protection. Explore cost-effective and compliant…

2 days ago

Innovative Strategies for Basement Waterproofing Success

Key Takeaways Basement waterproofing involves multiple strategies for long-term success. Proper drainage and sealing are…

2 days ago

Revolutionizing Food Display Solutions: Innovative Approaches for the Modern Era

Table of Contents Understanding the Basics of Food Display Latest Trends in Food Display Solutions…

2 days ago

How Technology is Reshaping Healthcare: A Journey Towards Better Patient Care

Key Takeaways Technology is rapidly transforming the healthcare industry. Benefits include improved patient outcomes, increased…

6 days ago

Innovative Strategies for Efficient Log Book Management in Trucking

Key Takeaways Understand the importance of efficient log book management for truck drivers. Explore the…

3 weeks ago

Best Crypto to Buy Now: Cryptocurrencies with the Most Potential in 2025

2025 is shaping up to be a monumental year for the cryptocurrency market. With Bitcoin…

3 weeks ago