Get Line From Text File

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi I need to Select a line from a text file can someone please tell the best
way of doing this .
TIA
Charles
 
depends on how big the text file is, how to identify which line you
require, is the line broken up into fields (columns), what is used to
identify each column eg comar
 
I've just been doing something like this, where the location of what i
wanted to copy out would change

So, i used a file search in the file to find an "anchor" - a word or
phrase that is fixed and doesn't change, then used that to move to the
necessary data.

For example, needed to find out how much was spent at midnight:

TIMEBAND REPORT
0000 321
0001 564
0002 654

etc

Searched for "TIMEBAND REPORT" then moved down a line.
 
The text file will one that I create with lines of information that I would
like to read by line, I need the best syntax of retrieving any line ramdomly
from the file.
TIA
Charles
 
Try this for starters

I am assuming you do not have more than 65536 rows in the text file

Sub Macro1()
Const conFile As String = "D:\MSN Hotmail - Message.txt"
Const conFind As String = "TIMEBAND REPORT"
Dim lRow As Long

Workbooks.OpenText Filename:=conFile, Origin:= _
xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False
Semicolon:=False _
, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
TrailingMinusNumbers:=True
lRow = Cells.Find(What:=conFind, After:=ActiveCell, LookIn:=xlFormula
_
, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row

MsgBox Cells(lRow + 1, 1).Value

End Su
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top