我正在尝试下面的代码但由于某种原因,它不像逻辑建议的那样工作。
myTxt = Application.GetOpenFilename("Text Files,*.txt", , , , True)
On Error Resume Next
filecount = UBound(myTxt)
On Error GoTo 0
If filecount = 0 Then MsgBox "No text file selected. Exiting now.", _
vbExclamation: Exit Sub
For i = LBound(myTxt) To filecount
fnum = FreeFile()
Debug.Print fnum
Open myTxt(i) For Input As #fnum
lines = Split(Input$(LOF(fnum), #fnum), vbNewLine)
Close #fnum
'~~> Do stuffs here for text parsing...
Next
基本上,我想处理多个.txt
文件,打开并加载到数组
然后关闭,在数组上执行操作并重复后续文件的步骤
但它只给我第一个文件的解析结果
我在代码中遗漏了什么吗?这样做的正确方法是什么?
答案 0 :(得分:1)
试试这个版本(TRIED AND TESTED)。
Option Explicit
Sub Sample()
Dim MyData As String, strData() As String
Dim i As Long, filecount As Long
Dim myTxt
myTxt = Application.GetOpenFilename("Text Files,*.txt", , , , True)
On Error Resume Next
filecount = UBound(myTxt)
On Error GoTo 0
If filecount = 0 Then MsgBox "No text file selected. Exiting now.", _
vbExclamation: Exit Sub
For i = LBound(myTxt) To filecount
'Debug.Print myTxt(i)
Open myTxt(i) For Binary As #1
MyData = Space$(LOF(1))
Get #1, , MyData
Close #1
strData() = Split(MyData, vbCrLf)
'Debug.Print UBound(strData)
'~~> Do stuffs here with strData for text parsing...
Next
End Sub
答案 1 :(得分:0)
作为答案发布,以避免混淆:是什么让代码避免解析所有文本文件:
For j = LBound(lines) To UBound(lines)
If mydate = 0 Then If IsDate(Trim(lines(j))) Then mydate = CDate(Trim(lines(j)))
If UpdatePage(lines(j)) Then page = page + 1
Select Case True
Case page = 1
Case page = 2
.
.
Case page = 4
End Select
End If
Next
'page = 0
'mydate = 0
注释行是我在原始代码中遗漏的内容
因此Select Case
例程被忽略,因为page
没有重置
所以代码本身真的没有问题
目前的代码以及Siddharth Rout发布的内容正在发挥作用。