Home > How-To Library > Text File Handling

How to delete a line for a given line number from a text file

**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * ****************************************************************
Private Function DeleteLine(ByVal sFile As String, ByVal lLine As Long) As Boolean Dim sLines() As String, FF As Integer, n As Long If Len(Dir(sFile)) = 0 Or lLine < 1 Then Exit Function FF = FreeFile Open sFile For Input As #FF sLines = Split(Input(LOF(FF), #FF), vbCrLf) Close #FF If UBound(sLines) < lLine - 1 Then Exit Function Open sFile For Output As #FF For n = 0 To UBound(sLines) If Not n = lLine - 1 Then Print #FF, sLines(n) Next n Close #FF DeleteLine = True End Function

If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.