Excel - Read - Parse Text File Line by Line in VBA - Stack Overflow
Excel - Read - Parse Text File Line by Line in VBA - Stack Overflow
excelRead/ParsetextfilelinebylineinVBAStackOverflow
signup
login
tour
help
Signup
StackOverflowisacommunityof4.7millionprogrammers,justlikeyou,helpingeachother.Jointhemitonlytakesaminute:
Read/ParsetextfilelinebylineinVBA
I'mtryingtoparseatextdocumentusingVBAandreturnthepathgiveninthetextfile.
Forexample,thetextfilewouldlooklike:
*Blahblahinstructions
*Blahblahinstructionsonline2
G:\\Folder\...\data.xls
D:\\AnotherFolder\...\moredata.xls
IwanttheVBAtoload1lineatatime,andifitstartswitha * thenmovetothenextline(similartothatlinebeingcommented).Forthe
lineswithafilepath,Iwanttowritethatpathtocell,say A2 forthefirstpath, B2 forthenext,etc.
ThemainthingsIwashopingtohaveansweredwere:
1.Whatisthebest/simplewaytoreadthroughatextfileusingVBA?
2.HowcanIdothatlinebyline?
excel vba excelvba
editedApr28'14at5:35
askedJul17'12at18:33
L42
11.5k
dancran
5
16
39
126
12
4Answers
forthemostbasicreadofatextfile,use
open
example:
DimFileNumAsInteger
DimDataLineAsString
FileNum=FreeFile()
Open"Filename"ForInputAs#FileNum
WhileNotEOF(FileNum)
LineInput#FileNum,DataLine'readindata1lineatatime
'decidewhattodowithdataline,
'dependingonwhatprocessingyouneedtodoforeachcase
Wend
editedMar5'13at14:07
answeredJul17'12at18:48
SeanC
10.2k
17
42
1 I'malittlelatetotheparty,butLineInputhasproblemswithanythingotherthanstrictlyaCRorCRLF
combination(i.e.LFonitsown)FSOhasnosuchproblems(butyeah,isprobablyalittlebitslower)
Cor_Blimey Aug20'13at21:28
6 RememberClose#FileNumattheend!MagnusSmithMay26'14at11:15
Foranyoneelsewondering: DataLine excludestheterminating CR or CRLF (source)FelipeMay11at
15:35
http://stackoverflow.com/questions/11528694/readparsetextfilelinebylineinvba
1/3
12/22/2015
excelRead/ParsetextfilelinebylineinVBAStackOverflow
Forcompletenessworkingwiththedataloadedintomemory
dimhfAsinteger:hf=freefile
dimlines()asstring,iaslong
open"c:\bla\bla.bla"forinputas#hf
lines=Split(input$(LOF(hf),#hf),vbnewline)
close#hf
fori=0toubound(lines)
debug.?"Line";i;"=";lines(i)
next
answeredJul18'12at13:06
AlexK.
92.7k
12
123
165
1 Thisisthecorrectanswer.YoucandothesameinFileSystemObject,butFSOwilloftenfailtorundueto
securityrestrictions,anditissignificantlyslower.NileJul18'12at17:00
1 ...butassumesthefilewillfitintomemory ChrisKimptonMay31'13at7:36
IfindtheFileSystemObjectwithaTestStreamtobetheeasiestwaytoreadfiles
DimfsoAsFileSystemObject:Setfso=NewFileSystemObject
SettxtStream=fso.OpenTextFile(filePath,ForReading,False)
NOTE:ThisrequiresareferencetoMicrosoftScriptingRuntime.
editedJun19'13at17:18
answeredJul17'12at19:59
Brad
6,671
15
45
YouCanusethiscodetoreadlinebylineintextfileandYoucouldalsocheckaboutthefirst
characteris"*"thenyoucanleavethat..
PublicSubTest()
DimReadDataasString
Open"C:\satheesh\myfile\file.txt"ForInputAs#1
DoUntilEOF(1)
LineInput#1,ReadData'AddingLinetoreadthewholeline,notonlyfirst128
positions
IfNotLeft(ReadData,1)="*"then
''youcanwritethevariableReadDataintothedatabaseorfile
EndIf
Loop
Close#1
EndSub
editedApr29at16:22
answeredDec26'12at17:09
GJK
12.1k
satheeshkumar
3
18
51
http://stackoverflow.com/questions/11528694/readparsetextfilelinebylineinvba
60
2/3
12/22/2015
excelRead/ParsetextfilelinebylineinVBAStackOverflow
http://stackoverflow.com/questions/11528694/readparsetextfilelinebylineinvba
3/3