软件:EXCLE
版本:2010
您好
我运用EXCEL VBA撰写一个宏指令
内容是读取100个JSON档案后再将其内容重行排版后
输出成1个TXT档(目的是为了格式转换)
但是我发现开启这个EXCEL并第1次执行时耗时6.8秒
在未关闭EXCEL档案情况下,执行第2次时耗时7.7秒
依此类推,第3次耗时8.9秒、第4次耗时10.64秒、第5次耗时13.56秒...
请问原因是什么呢?因为我原始资料有达数十万笔,如果依照这样的速率
将无法继续执行下去
感谢回复
宏内容如下:
Sub test()
Dim Time0#
Time0 = Timer
Dim OutputFilePath As String
OutputFilePath = "D:\output.txt"
Open OutputFilePath For Output As #1
len1 = WorksheetFunction.CountA(Range("'工作表1'!A:A"))
For i = 1 To 100
On Error Resume Next
num1 = WorksheetFunction.Find(",", 工作表1.Cells(i, 1))
numA = Mid(工作表1.Cells(i, 1), 1, num1 - 2)
numB = Mid(工作表1.Cells(i, 1), num1 - 1, 1)
num2 = WorksheetFunction.Find(",", 工作表1.Cells(i, 1), num1 + 1)
numC = Mid(工作表1.Cells(i, 1), num1 + 1, num2 - num1 - 1)
num3 = WorksheetFunction.Find(",", 工作表1.Cells(i, 1), num2 + 1)
numD = Mid(工作表1.Cells(i, 1), num2 + 1, num3 - num2 - 1)
num4 = WorksheetFunction.Find(",", 工作表1.Cells(i, 1), num3 + 1)
numE = Mid(工作表1.Cells(i, 1), num3 + 1, num4 - num3 - 1)
numF = Mid(工作表1.Cells(i, 1), num4 + 1, 8)
Sheets("工作表2").Select
filepath1 = "TEXT;D:\ " & 工作表1.Cells(i, 1)
With ActiveSheet.QueryTables.Add(Connection:= _
filepath1, Destination _
:=Range("$A" & 1))
.Name = 工作表1.Cells(i, 1)
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
num5 = Len(工作表2.Cells(1, 11))
numG = Mid(工作表2.Cells(1, 11), 9, num5 - 9)
工作表2.Rows("1:1").Select
Selection.Delete Shift:=xlToLeft
Print #1, 工作表1.Cells(i, 1); " "; numA; " "; numB; " "; numC; " ";
numD; " "; numE; " "; numF; " "; numG
Next
Close #1
MsgBox "执行时间 " & Timer - Time0 & " 秒" & vbCrLf & "平均时间" & (Timer -
Time0) / 100 & "秒"
End Sub