可执行宏命令。打开总表1.xlsm文件,执行宏ThisWorkbook.Macro2代码如下:Sub Macro2()Dim fileDir As String '文本文件目录Dim fileName As String '要打开的文本文件名Dim serverNo As StringDim serverDate As StringDim dataSum As Long '要合并的文本记录数Dim dataTotalOld As Long '汇总表中未合并时的记录条数

Application.ScreenUpdating = FalsefileDir = ActiveWorkbook.Path & "/"fileName = Dir(fileDir, 7)Do While fileName "" And Right(fileName, 3) = "txt"'fileName = "1 4-10.txt"'获取服务器号和日期serverNo = Left(fileName, InStr(1, fileName, " ") - 1) & "服"serverDate = Mid(fileName, InStr(1, fileName, " ") + 1)serverDate = Replace(serverDate, "-", "月")serverDate = Replace(serverDate, ".txt", "日")

Workbooks.OpenText fileName:= _ActiveWorkbook.Path + Application.PathSeparator & fileName, Origin:=936, _StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _Array(3, 1), Array(4, 1)), TrailingMinusNumbers:=True

dataSum = Range("a65535").End(xlUp).RowRange("A1:D" & dataSum).Select

Range("A1:D" & dataSum).SelectSelection.Copy

'总表1.xlsm为要合并后的启动宏工作表Workbooks("总表1.xlsm").Activate

dataTotalOld = Range("a65535").End(xlUp).Row + 1If dataTotalOld = 2 Then dataTotalOld = 1 '第一次使用Range("C" & dataTotalOld).SelectActiveSheet.Paste

Range("A" & dataTotalOld) = serverNoRange("B" & dataTotalOld) = serverDateRange("A" & dataTotalOld + 1) = serverNoRange("B" & dataTotalOld + 1) = serverDate

Range("A" & dataTotalOld & ":B" & dataTotalOld + 1).SelectSelection.AutoFill Destination:=Range("A" & dataTotalOld & ":B" & dataTotalOld + dataSum - 1), Type:=xlFillDefault

Workbooks(fileName).Close savechanges:=False

fileName = DirDebug.Print fileNameLoop

Application.ScreenUpdating = TrueEnd Sub