Delete lines of a .txt file containing specific letters using VBA ?

Pramodpandit123

New Member
Joined
Apr 18, 2020
Messages
30
Office Version
  1. 2016
Platform
  1. Windows
Let me give you an example :
Image=Image1
Image=Image2
Image=Image3

TextImage=Image1
CSVImage=Image2
StringImage=Image2


I have a text file containing these words. How can i delete lines from this text file which contains "Image=" in its line i.e. Line 1st,2nd and 3rd should be deleted but Line 5th,6th and 7th should be as it is. How can i achieve it ? I am not proficient on VBA but i tried to use "Replace" on following code that i took reference from internet.

VBA Code:
Dim strFileName As String
Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String

sFileName = "C:\Users\Epic\Desktop\PROJECT\PROJECT.txt"
returnvalue = Shell("notepad.exe " & sFileName, vbNormalFocus)

iFileNum = FreeFile
Open sFileName For Input As iFileNum

Do Until EOF(iFileNum)
    Line Input #iFileNum, sBuf
    sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum

sTemp = [B]Replace(sTemp, "[I]Image=[/I]", "")[/B]
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
How about something like this?

VBA Code:
Sub DeleteLines()
    Dim strFileName As String
    Dim sBuf As String
    Dim sTemp As String
    Dim iFileNum As Integer
    Dim sFileName As String
    
    sFileName = "C:\Users\Epic\Desktop\PROJECT\PROJECT.txt"
    
    iFileNum = FreeFile
    Open sFileName For Input As iFileNum
    
    Do Until EOF(iFileNum)
        Line Input #iFileNum, sBuf
        If LCase(Left(sBuf, 5)) = "image" Then
            sBuf = ""
        End If
        sTemp = sTemp & sBuf & vbCrLf
    Loop
    Close #iFileNum
    
    'Remove the last vbCrLf
    sTemp = Left(sTemp, Len(sTemp) - 2)
    
    'Write sTemp back to file
    iFileNum = FreeFile
    Open sFileName For Output As iFileNum
    Print #iFileNum, sTemp
    Close #iFileNum
End Sub
 
Upvote 0
Solution
According to VBA basics as a beginner starter :​
VBA Code:
Sub Demo1()
      Const T = "C:\Users\Epic\Desktop\PROJECT\PROJECT.txt"
        Dim F%, S$(), V
    If Dir(T) > "" Then
            F% = FreeFile
            Open T For Input As #F
            S = Split(Input(LOF(F), #F), vbCrLf)
            Close #F
            Open T For Output As #F
        For Each V In S
            If Not V Like "Image=*" Then Print #F, V
        Next
            Close #F
    End If
End Sub
 
Upvote 0
How about something like this?

VBA Code:
Sub DeleteLines()
    Dim strFileName As String
    Dim sBuf As String
    Dim sTemp As String
    Dim iFileNum As Integer
    Dim sFileName As String
   
    sFileName = "C:\Users\Epic\Desktop\PROJECT\PROJECT.txt"
   
    iFileNum = FreeFile
    Open sFileName For Input As iFileNum
   
    Do Until EOF(iFileNum)
        Line Input #iFileNum, sBuf
        If LCase(Left(sBuf, 5)) = "image" Then
            sBuf = ""
        End If
        sTemp = sTemp & sBuf & vbCrLf
    Loop
    Close #iFileNum
   
    'Remove the last vbCrLf
    sTemp = Left(sTemp, Len(sTemp) - 2)
   
    'Write sTemp back to file
    iFileNum = FreeFile
    Open sFileName For Output As iFileNum
    Print #iFileNum, sTemp
    Close #iFileNum
End Sub
Thanks ! It Solved the stated problem.
Also for the same problem how can we copy/replace, only the first found "image" line with some texts on different lines and delete the rest as above.
For eg: Here, Image=Image1 line is the 1st line with string "image" so it is to be replaced with "Sample Text1" (on line 1) & "Sample Text2" (on line 2).It may look like this :
" Sample Text1
Sample Text2

TextImage=Image1
CSVImage=Image2
StringImage=Image2"
 
Upvote 0
VBA Code:
Sub Demo2()
      Const T = "C:\Users\Epic\Desktop\PROJECT\PROJECT.txt"
        Dim F%, S$(), B%, L&
    If Dir(T) > "" Then
            F% = FreeFile
            Open T For Input As #F
            S = Split(Input(LOF(F), #F), vbCrLf)
            Close #F
            Open T For Output As #F
            B = 1
        For L = 0 To UBound(S) + (S(UBound(S)) = "")
            If S(L) Like "Image=*" Then
                If B Then B = 0: Print #F, "Sample Text1"; vbCrLf; "Sample Text2"
            Else
                Print #F, S(L)
            End If
        Next
            Close #F
    End If
End Sub
 
Upvote 0
Thank you ! Worked like a charm ?
VBA Code:
Sub Demo2()
      Const T = "C:\Users\Epic\Desktop\PROJECT\PROJECT.txt"
        Dim F%, S$(), B%, L&
    If Dir(T) > "" Then
            F% = FreeFile
            Open T For Input As #F
            S = Split(Input(LOF(F), #F), vbCrLf)
            Close #F
            Open T For Output As #F
            B = 1
        For L = 0 To UBound(S) + (S(UBound(S)) = "")
            If S(L) Like "Image=*" Then
                If B Then B = 0: Print #F, "Sample Text1"; vbCrLf; "Sample Text2"
            Else
                Print #F, S(L)
            End If
        Next
            Close #F
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,755
Members
449,049
Latest member
excelknuckles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top