|
Sub txt到excel()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim lines, cols ''定义两个数组
Dim i As Integer, j As Integer, k As Integer, q As Integer ''定义四个数值型变量
Dim fileOpenName ''定义一个文本型变量,获取文件路径和文件名称
Dim arr()
ReDim arr(1 To 10000, 1 To 9)
f = Dir(ThisWorkbook.Path & "\*.txt")
Worksheets("Sheet1").UsedRange.Offset(1).ClearContents ''清空原来的数据
Worksheets("Sheet1").Columns("g:g").NumberFormatLocal = "@" ''设置A列为文本格式
Do While f <> ""
n = n + 1
arr(n, 1) = n
fileOpenName = ThisWorkbook.Path & "\" & f
'Open fileOpenName For Input As #1 ''打开选择的文本文件
Set wb = Workbooks.Open(fileOpenName)
ar = wb.Worksheets(1).[a1].CurrentRegion
'lines = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf) ''把文本文件的数据赋值给数组lines
'k = UBound(lines) ' + 1 '文件的行数
'遍历每一行
'Close #1 ''关闭打开的文本文件
wb.Close False
mc = Trim(Split(f, ".")(0))
k = UBound(ar)
If Len(mc) > 10 Then arr(n, 9) = Mid(mc, 4, Len(mc) - 3)
For i = 1 To k ''循环数组行
If InStr(ar(i, 1), ":") > 0 Then ''判断该行是否有逗号
rr = Split(ar(i, 1), ":")(1) '以逗号作为分隔,将每行字符分割,分隔符可根据实际情况自己修改
arr(n, i + 1) = rr
End If
Next i
f = Dir
Loop
Sheet1.[a2].Resize(n, UBound(arr, 2)) = arr
MsgBox "获取了" & n & "行数据"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub |
评分
-
1
查看全部评分
-
|