从文本文件中读取?
好的,我回来了(仍然在玩习惯它)我有一个关于从文本文件中读取的问题。
我有一个文本文件,其中包含如下行:
A-ANNO-DIMS;见证/延伸线、尺寸终止符、尺寸文本;连续;35;7;1;层创建器;
(所有一行没有换行)
我也有这个代码
Public Sub ReadFile()
Dim LayerLine As String
Open "C:\Acad ToolBOX\Layer Creator\Layer Files\Architectural.lyr" For Input As #1 'Open file for input
Do While Not EOF(1) 'Loop until end of file
Input #1, LayerLine 'Read data into two variables
Debug.Print LayerLine 'Print data to the Immediate window
Loop
Close #1 'Close file
End Sub
问题是,当我运行它时,它在逗号上很糟糕,我怎么能把它弄到
a.读行作为一行
b.读行并在分号处分开?
- 这将是最好的选择,然后我可以为每个变量设置变量(列出数组吗?
无论如何,这是来自我编写的layercreator程序,我可能会尝试将其移植到vba。
断续器
**** Hidden Message ***** 时间不多,所以我希望我做对了...我使用这样的东西....
Public Sub ReadMyFile()
Dim inputfile As String
inputfile = "C:\myfile.txt"
Dim DataFile As Integer
DataFile = FreeFile
Dim Dataline As String
Open inputfile For Input As #DataFile
While Not EOF(DataFile)
Line Input #DataFile, Dataline
' now you've got the line so do your stuff here
Wend
Close #DataFile
end sub
您可能希望查看 replace方法,将行中的逗号替换为分号,反之亦然。然后,您可以使用split函数从您的行返回一个数组
,这将用逗号替换分号
replace(dataline,";",",",,,vbBinaryCompare)
,然后使用该分隔符将您的行拆分为数组
dim LineArray as variant
LineArray =Split(dataline,",",,vbBinaryCompare)
这里有一种方法,将每一行传递给一个函数代码4]
页:
[1]