|
'稍作修改,自己再测试一下
Option Explicit
Sub test()
Dim filename(), i, j, k, m, arr, pos
If Not getfilename(filename, ThisWorkbook.Path, ".dat") Then MsgBox "!": Exit Sub
ReDim brr(1 To Rows.Count, 1 To 1)
For i = 1 To UBound(filename)
Open filename(i) For Input As #1
arr = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbNewLine)
Close #1
pos = 0
For j = UBound(arr) To 0 Step -1 '去除文件尾空行
If Len(arr(j)) Then pos = j: Exit For
Next
For j = 0 To pos: m = m + 1: brr(m, 1) = arr(j): Next
Next
With [a:a]
.ClearContents
If m > 0 Then .Resize(m) = brr
End With
End Sub
Function getfilename(filename, pth, mark) As Boolean
Dim f, n
If Right(pth, 1) <> "\" Then pth = pth & "\"
f = Dir(pth & "*.*")
Do While Len(f) > 0
If LCase(Right(f, Len(mark))) = LCase(mark) Then
n = n + 1: ReDim Preserve filename(1 To n)
filename(n) = pth & f
End If
f = Dir
Loop
If n > 0 Then getfilename = True
End Function |
|