You're getting hung up on the empty line at the end of the file. Just add a check to see if the line is equal to an empty string.
Code:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Using sr As New StreamReader("C:\Temp\today.txt"),
sw As New StreamWriter("C:\Temp\formatted.txt")
Dim sampleNo As String = ""
For Each line As String In sr.ReadToEnd.Split(Environment.NewLine)
If line.Trim.StartsWith("SAMPLE NO") Then
sampleNo = line.Split(": ")(1).Trim
Else
If line.Trim <> "" Then
Dim elements() As String = line.Split({" "}, StringSplitOptions.RemoveEmptyEntries)
sw.WriteLine(String.Format("{0}|{1}|{2}", sampleNo, elements(0), elements(1)))
End If
End If
Next
End Using
End Sub
End Class
Bookmarks