vba help - Read Text file two line one go

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

In a text file can we Read both line one go. in excel we have offset Option.
Read both line and take Action.


If InStr(strline, "Rejected") > 0 And InStr(Nextline, "Error") > 0 Then

end if

Sub test()

Dim fso As Scripting.FileSystemObject
Dim tsTxtFile As Scripting.TextStream
Dim strline As String

Set fso = New Scripting.FileSystemObject
Set tsTxtFile = fso_OpenTextFile("C:\test\test.txt", ForReading, False, TristateMixed)

With tsTxtFile

Do Until .AtEndOfStream

strline = tsTxtFile.ReadLine

If InStr(strline, "Rejected") > 0 And InStr(Nextline, "Error") > 0 Then ' Nextline is offset


End If

Loop


Thanks
mg
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I don't use the Scripting.FileSystemObject myself, but given has a ReadLine method, can't you just add this...

Nextline = tsTxtFile.ReadLine

immediately after this line...

strline = tsTxtFile.ReadLine

inside your loop?
 
Upvote 0
Hi Rick,

through loop we can do it.

But Reading two lines in one go option not available.
skip option is there but it skips next line instead of existing line.



Thanks
mg
 
Upvote 0
Did what I suggested not work for you (it should read two lines for each loop)?
 
Upvote 0

Forum statistics

Threads
1,215,461
Messages
6,124,956
Members
449,200
Latest member
indiansth

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