|
- Sub test()
- Dim fso, fp, reg, mh, arr, brr, f, strf, col%, str1$, firstv$, n&, k%, u%
- Set fso = CreateObject("scripting.filesystemobject")
- Set fp = fso.getfolder(ThisWorkbook.Path)
- Set reg = CreateObject("vbscript.regexp")
- reg.Pattern = "\d+(?:\.\d+)?(?=\r)"
- reg.Global = True
- col = Application.Round(fp.Files.Count / 2, 0)
- ReDim arr(1 To col, 1 To 1)
- ReDim brr(1 To col, 1 To 1)
- For Each f In fp.Files
- If fso.getextensionname(f) = "txt" Then
- Set strf = fso.opentextfile(f)
- str1 = strf.ReadAll
- n = strf.Line
- firstv = Split(fso.getfilename(f), ".")(0)
- strf.Close
- Set mh = reg.Execute(str1)
- If firstv Mod 2 = 0 Then
- k = k + 1
- If UBound(arr, 2) < n Then ReDim Preserve arr(1 To col, 1 To n)
- arr(k, 1) = firstv
- For i = 0 To mh.Count - 1
- arr(k, i + 2) = mh(i)
- Next
- Else
- u = u + 1
- If UBound(brr, 2) < n Then ReDim Preserve brr(1 To col, 1 To n)
- brr(u, 1) = firstv
- For i = 0 To mh.Count - 1
- brr(u, i + 2) = mh(i)
- Next
- End If
- End If
- Next
- Set reg = Nothing
- Set fso = Nothing
- Sheets("sheet1").Range("a2").Resize(UBound(arr, 2), UBound(arr)) = Application.Transpose(arr)
- Sheets("sheet2").Range("a2").Resize(UBound(brr, 2), UBound(brr)) = Application.Transpose(brr)
- End Sub
复制代码 |
|