|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub test3()
- Dim n As Byte
- Dim vResult$(), strFileName$, str$, st$, br$()
- Dim nums&, y&, x&, r&, k&
-
-
- nums = 242 * 80
- ReDim vResult(1 To 200, 1 To 4)
-
- strFileName = Dir(ThisWorkbook.Path & "" & "*.txt")
- r = 1
- Do Until strFileName = ""
- n = FreeFile
- Open ThisWorkbook.Path & "" & strFileName For Input As #n
- ' Debug.Print strFileName; LOF(n) vbCrLf
-
- If LOF(n) > nums Then
- Seek #n, LOF(n) - nums + 1
- str = StrConv(InputB(nums, #n), vbUnicode)
- Else
- str = StrConv(InputB(LOF(n), #n), vbUnicode)
- End If
-
- Close #n
-
- vResult(r, UBound(vResult, 2)) = Mid(strFileName, 4, 6)
- k = InStrRev(str, vbCrLf, Len(str) - 100)
- st = Mid(str, k + 2, 10)
- x = InStr(str, st)
- y = InStr(x + 20, str, vbCrLf)
-
- br = Split(Mid(str, x, y - x), vbTab)
- vResult(r, 1) = br(LBound(br))
- vResult(r, 2) = br(LBound(br) + 6)
- vResult(r, 3) = br(LBound(br) + 7)
-
- r = r + 1
- strFileName = Dir
-
- Loop
-
- Sheets("Sheet1").[a2].Resize(r, UBound(vResult, 2)) = vResult
-
- End Sub
复制代码
文件打开文件的优化不太容易,根据数据特点,把读取整个文件改为读取最后一块数据,由于要处理的数据量变小,所以速度还是有所提升 |
评分
-
2
查看全部评分
-
|