VBA怎樣讀入txt?

VBA怎樣讀入txt?使用者67897033607092020-01-05 08:16:40

PublicFunctionopenfile(ByValfilepathAsString)AsString‘讀入檔案函式

DimsAsString

OpenfilepathForInputAs#1

WhileNotEOF(1)

LineInput#1,sline

s=s&sline&vbCrLf

Wend

Close#1

openfile=s

EndFunction

PrivateSubCommand1_Click()

DimvStr

vStr=Split(openfile(“C:\test。txt”),vbCrLf)

IfUBound(vStr)<>0Then

MsgBox“第8行資料是”&vStr(8)’這個8自己定義

EndIf

EndSub

當然這段程式碼也有侷限性在讀取小文字時就有用如果是過50KB的文字估計程式會down掉

那你可以使用類似

OpenfilepathForInputAs#1

DoWhileNotEOF(1)

LineInput#1,Stream

的方法來流式讀取