可执行宏命令。
打开总表1.xlsm文件,执行宏ThisWorkbook.Macro2
代码如下:
Sub Macro2()
Dim fileDir As String ‘文本文件目录
Dim fileName As String ‘要打开的文本文件名
Dim serverNo As String
Dim serverDate As String
Dim dataSum As Long ‘要合并的文本记录数
Dim dataTotalOld As Long ‘汇总表中未合并时的记录条数
Application.ScreenUpdating = False
fileDir = 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).Row
Range(“A1:D” & dataSum).Select
Range(“A1:D” & dataSum).Select
Selection.Copy
‘总表1.xlsm为要合并后的启动宏工作表
Workbooks(“总表1.xlsm”).Activate
dataTotalOld = Range(“a65535”).End(xlUp).Row + 1
If dataTotalOld = 2 Then dataTotalOld = 1 ‘第一次使用
Range(“C” & dataTotalOld).Select
ActiveSheet.Paste
Range(“A” & dataTotalOld) = serverNo
Range(“B” & dataTotalOld) = serverDate
Range(“A” & dataTotalOld + 1) = serverNo
Range(“B” & dataTotalOld + 1) = serverDate
Range(“A” & dataTotalOld & “:B” & dataTotalOld + 1).Select
Selection.AutoFill Destination:=Range(“A” & dataTotalOld & “:B” & dataTotalOld + dataSum – 1), Type:=xlFillDefault
Workbooks(fileName).Close savechanges:=False
fileName = Dir
Debug.Print fileName
Loop
Application.ScreenUpdating = True
End Sub