I have a texfile that populates a textbox on a userform. I would like to remove all blank lines in the string including those at the end if they exist, before populating the textbox. I'm reading the entire file at once into the string, not line by line.
Is there any way to edit the string called Text to remove the blank lines before populating the textbox? I'm looking for 2 carriage return characters in a row, and if so then remove one of them, but I don't know how to code that. This is in the userform activate section.
If I read the textfile line by line, I don't know how to populate the textbox that way and remove the blank lines.
Private Sub UserForm_Activate()
Dim FileName As String
Dim FSO As Object
Dim Text As String
Dim TextFile As Object
FileName = "C:\Documents and Settings\smith\sample.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TextFile = FSO.OpenTextFile(FileName, 1, False, -2)
'Read the file into a string
Text = TextFile.ReadAll
TextFile.Close
' Edit out the blank lines here
Me.TextBox1 = Text
End Sub
Please see the example workbook and textfile.
Bookmarks