我一直在研究如何更快地完成以下任务,但我无法......
你可以帮我解决这个问题吗?它基本上是一个将文本文件转换为工作表的解析器,但问题是文本文件有大约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
输入文本文件的示例是:
SET ALMBLKPARA:AID = 20031,BLKPRD = 0,CNTRISTHRD = 0,CNTSTLTHRD = 0,TMRISTHRD = 0,TMSTLTHRD = 0;
其中ALMBLKPARA必须是工作表的名称,AID,BLKPRD,CNTRISTHRD,CNTSTLTHRD,TMRISTHRD和TMSTLTHRD列标题,每个“=”后具有相应的值。
我非常感谢你的帮助。
谢谢, 塞萨尔