![]() ![]() 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. Set myFile = fso.CreateTextFile(filePath) With the reading of the text file completed, we close the file and write the text string to the new file.Įxcel VBA Tutorial : Workbook Events and Procedures myFile.closeįilePath = ActiveWorkbook.path & "filesblanksRemoved.txt" If the line meets our parameters, then we add it with a new line character to the txt string and continue the loop. If countBlanks < includeBlanks Then includeLine = True ![]() The number of blank lines are counted until a non-blank line is found and then the counter is set back to zero. 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. Now we can read the file and tell the code which lines to write to the new file. How To Import Data Into an Excel Spreadsheet If we wanted to remove every blank line we’d set the variable to 0. The initial setting for blank lines is set at 1, so we don’t upset any paragraph breaks. ' The number of blank lines to include in the new file ' The text string to write the included lines to We’ll start by defining several variables and give them initial values. Now, we can read each line in the file but first we need to consider the code we need to discard the blank lines. Dim fso As Scripting.FileSystemObjectįilePath = ActiveWorkbook.path & "filesblanks.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. VBA Macro to Compare Two Files to Determine if They are Identicalįirst, we’ll access the file system object (FSO) and open the file, which we’ve named “blanks.txt”. Then, we’ll write the text string back into a new 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. This article will show you how to remove a set number of blank lines from a text file using some common sense VBA coding. Part of the problem is that you don’t want to remove every blank line otherwise it might upset the formatting of the document. If you’ve ever worked with text files, you’ll know how frustrating it can be to remove empty lines. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |