Sub test() Dim objshell As Object, objfolder As Object Dim Mypath As String, Myfile As String Dim r As Integer
'得到合并路径(Mypath) Set objshell = CreateObject("Shell.Application") Set objfolder = objshell.BrowseForFolder(0, "选择文件夹", 0, 0) If objfolder Is Nothing Then End Else Mypath = objfolder.self.Path & "\" End If Set objfolder = Nothing Set objshell = Nothing
Workbooks.Add Myfile = Dir(Mypath & "\*.txt") Do While Myfile <> ""
If r = 0 Then r = 1 '首次 Else r = Range("A1").End(xlDown).Row + 1 '不是首次 End If
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & Mypath & Myfile, Destination:=Cells(r, 1)) .TextFileOtherDelimiter = "|" .TextFileColumnDataTypes = Array(2, 1, 1) .Refresh BackgroundQuery:=False End With