|
Sub D20合并文本()
'made by fxw
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Dim newwb As Workbook
Set newwb = Workbooks.Add
newwb.Application.ActiveWindow.Caption = "MergeTXT.xls"
With fd
.Filters.Clear
.Filters.Add "文本文件", "*.txt", 1
.Filters.Add "所有文件", "*.*", 2
.Title = " 请选择要合并的txt文件 "
If .Show = -1 Then
Application.ScreenUpdating = False
Dim vrtSelectedItem As Variant
Dim i As Integer
i = 1
For Each vrtSelectedItem In .SelectedItems
Dim tempwb As Workbook
Set tempwb = Workbooks.Open(vrtSelectedItem)
tempwb.Worksheets(1).Copy Before:=newwb.Worksheets(i)
newwb.Worksheets(i).Name = VBA.Replace(tempwb.Name, ".txt", "")
tempwb.Close savechanges:=False
i = i + 1
Next vrtSelectedItem
Else:
newwb.Close savechanges:=False
Exit Sub
End If
End With
Set fd = Nothing
If ActiveWorkbook.Sheets.Count > 3 Then
Sheets("Sheet1").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Sheets("Sheet2").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
Sheets("Sheet3").Select
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True
End If
Sheets(1).Select
Application.ScreenUpdating = True
End Sub
这段代码是读取文件输出在工作表里。但是他是读取一个文件就输出在一个新的工作表里。我想它不管读取多少个txt文件都输出在同一个工作表上。
谁帮忙修改下,谢谢!
|
|