- Sub 宏1()
- Dim strPath$, strFile$, dataFile$, myRng As Range, dataRng As Range, i%
- strPath = ThisWorkbook.Path & ""
- strFile = Dir(strPath & "*.txt")
- Do While strFile <> ""
- i = i + 1
- dataFile = "TEXT;" & strPath & strFile
- Set myRng = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
- Set dataRng = IIf(i = 1, Range("$A$1"), myRng)
- With ActiveSheet.QueryTables.Add(Connection:=dataFile, Destination:=dataRng)
- .TextFilePlatform = 65001
- .TextFileStartRow = IIf(i = 1, 1, 2)
- .Refresh BackgroundQuery:=False
- End With
- strFile = Dir
- Loop
- End Sub
复制代码 |