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

The Benefits of Partnering with an IT-Managed Service Provider for Your Business

Table of Contents Introduction to IT Managed Service Providers Why Outsource IT Management? Cost-Effective Solutions…

2 months ago

Choosing the Right Thresholds for Your Home: A Comprehensive Guide

Key Takeaways: The importance of selecting the correct thresholds for different areas in your home…

4 months ago

Innovative Railing Gate Solutions for Modern Homes

Key Takeaways: The variety of railing gate designs can significantly enhance the aesthetic appeal of…

4 months ago

How To Choose the Perfect Vehicle for Extended Commutes

For many, commuting is an unavoidable part of daily life. But when that commute extends…

4 months ago

The Future of Mobility: Innovations in Automotive Technology

When you're on the road, you want to feel safe, comfortable, and like you have…

5 months ago

Clean Air Starts at Home: Tips for Maintaining Indoor Air Quality

Key Takeaways: Understanding the significance of indoor air quality. Identifying common pollutants in your home.…

5 months ago