我一直在研究一种更快的方法来完成以下任务,但我不能......
你能帮我吗?它基本上是一个将文本文件转换为表格的解析器,但问题是文本文件有大约 100000 行,而这段代码需要半个多小时才能运行!
对于文本文件的每一行,首先我检查工作表是否已经存在,然后检查列标题是否已经存在......如果是,它将数据填充到相应的字段中,如果不是,它会创建工作表和列.
我知道这段代码根本没有优化,但我不是程序员,所以这就是我可以完成这项工作的方式。
Open sFileName For Input As #1
While Not EOF(1)
Line Input #1, sLin
If Left(sLin, 3) = "ADD" Or Left(sLin, 3) = "SET" Or Left(sLin, 3) = "MOD" Then
sLinSplitted = Split(sLin, ",")
NumberOfParameters = UBound(sLinSplitted)
For p = 0 To NumberOfParameters
Select Case p
Case 0
Aux1 = Split(sLinSplitted(p), ":")
AuxTabl = Split(Aux1(0), " ")
Tabl = AuxTabl(1)
Aux2 = Split(Aux1(1), "=")
Parm = Aux2(0)
Parmval = Aux2(1)
If Tabl <> Tablpre Then
DoNotCreate = 0
For Each sh In ThisWorkbook.Worksheets
If sh.Name = Tabl Then
DoNotCreate = 1
GoTo Continue:
End If
Next
If DoNotCreate <> 1 Then
ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)).Name = Tabl
End If
Tablpre = Tabl
End If
继续:
Linh = ThisWorkbook.Sheets(Tabl).UsedRange.Rows.Count + 1
ThisWorkbook.Sheets(Tabl).Cells(1, 1) = "NE"
Case Is > 0
Aux2 = Split(sLinSplitted(p), "=")
Parm = Aux2(0)
Parmval = Aux2(1)
End Select
Parm = Replace(Parm, " ", "")
Parmval = Replace(Parmval, ";", "")
Set Strg = ThisWorkbook.Sheets(Tabl).Range("1:3").Find(Parm, LookAt:=xlWhole)
If Strg Is Nothing Then
With ThisWorkbook.Sheets(Tabl).Rows("1:1")
Set CT = .Find(What:="")
End With
column = CT.Column
Else
column = Strg.Column
End If
ThisWorkbook.Sheets(Tabl).Cells(1, column) = Parm
ThisWorkbook.Sheets(Tabl).Cells(Linh, column) = Parmval
Next
ThisWorkbook.Sheets(Tabl).Cells(Linh, 1) = NEName
If ThisWorkbook.Sheets(Tabl).Cells(2, 1) = "" Then
Linh = 2
Else
Temp = ThisWorkbook.Sheets(Tabl).UsedRange.Rows.Count
Linh = Temp + 1
End If
End If
Wend
Close
输入文本文件中的一个示例是:
设置 ALMBLKPARA:AID=20031, BLKPRD=0, CNTRISTHRD=0, CNTSTLTHRD=0, TMRISTHRD=0, TMSTLTHRD=0;
其中 ALMBLKPARA 必须是工作表的名称,AID、BLKPRD、CNTRISTHRD、CNTTSTLTHRD、TMRISTHRD 和 TMSTLTHRD 是每个“=”后具有相应值的列标题。
我真的很感谢你的帮助。
谢谢,塞萨尔