大家好,之前写过一个程序,是将多个工作簿合并成一个工作簿,不同的工作簿复制到不同的sheet,sheet以工作簿的名称命名,今天这个是将多个工作簿合并到一个sheet,并在对应的第一列标注工作簿名称。Application.ScreenUpdating = FalseApplication.DisplayAlerts = FalseDim myrow, lastrow As LongSet mySht = ThisWorkbook.Sheets('合并')mySht.Range('A2:E50000') = ''fname = Dir(ThisWorkbook.Path & '\*.xlsx')If fname <> ThisWorkbook.Name Then Workbooks.Open ThisWorkbook.Path & '\' & fname lastrow = .Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row '从第二行第一列到最后一行第四列复制到当前工作簿的第二行第二列 .Sheets(1).Range(Cells(2, 1), Cells(lastrow, 4)).Copy mySht.Cells(myrow, 2) '将被复制的工作表的名字(即部门名称)写入到对应人员的第一列mySht.Range(mySht.Cells(myrow, 1), mySht.Cells(myrow + lastrow - 1, 1)) = Split(fname, '.')(0) myrow = myrow + lastrow - 1 Workbooks(fname).Close savechanges:=False Application.DisplayAlerts = TrueApplication.ScreenUpdating = True ----------------------------------------
|