在VBA中逐行阅读/解析文本文件 [英] Read/Parse text file line by line in VBA
问题描述
我正在尝试使用VBA解析文本文档并返回文本文件中给出的路径。
例如,文本文件将如下所示:
I'm trying to parse a text document using VBA and return the path given in the text file.
For example, the text file would look like:
*Blah blah instructions
*Blah blah instructions on line 2
G:\\Folder\...\data.xls
D:\\AnotherFolder\...\moredata.xls
我希望VBA一次加载1行,如果它以$ code> * 开头,然后移动到下一行(类似于该行被注释)。对于具有文件路径的行,我想将该路径写入单元格,对于第一个路径 A2
B2
为下一个等。
I want the VBA to load 1 line at a time, and if it starts with a *
then move to the next line (similar to that line being commented). For the lines with a file path, I want to write that path to cell, say A2
for the first path, B2
for the next, etc.
我希望回答的主要事项是:
1.什么是最好的/简单的方式使用VBA阅读文本文件?
2.如何逐行执行?
The main things I was hoping to have answered were:
1. What is the best/simple way to read through a text file using VBA?
2. How can I do that line by line?
推荐答案
为了最基本的阅读文本文件,使用打开
for the most basic read of a text file, use open
示例:
Dim FileNum As Integer
Dim DataLine As String
FileNum = FreeFile()
Open "Filename" For Input As #FileNum
While Not EOF(FileNum)
Line Input #FileNum, DataLine ' read in data 1 line at a time
' decide what to do with dataline,
' depending on what processing you need to do for each case
Wend
这篇关于在VBA中逐行阅读/解析文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!