[英]copy data from multiple worksheets in multiple workbooks, all into single master workbook
I am new to macro and needs help. 我是宏的新手,需要帮助。 I have few workbooks in a folder and each workbook has four worksheets. 我的文件夹中没有几个工作簿,每个工作簿都有四个工作表。 now I want a mocro which copy data from each workbook (worksheet wise) and past in my master workbook (worksheet wise) means data of sheet1 shoud be pasted one below the other in my master workbook in sheet1 and sheet 2 respectively.*Workbook name could be anything in folder. 现在我想要一个Mocro,它可以复制每个工作簿中的数据(明智的工作表),并复制到我的主工作簿中(明智的工作表),这意味着工作表1的数据应分别粘贴在我的主工作簿中的sheet1和工作表2中。可以是文件夹中的任何内容。 Can anyone help me with entire code to do that? 任何人都可以帮助我完成整个代码吗? I have macro to colate data from one sheet to my assigned sheet but it copy paste data from open sheet only not by sheet name wise. 我有宏可以将数据从一张纸整理到我指定的纸上,但是它只能从打开的纸上复制粘贴数据,而不是按纸名来复制。 Can anyone help to make corrections in my below code : 任何人都可以在我的以下代码中进行更正:
Sub Ref_Doc_Collation()
Dim MyFile As String
Dim erow
Dim Filepath As String
Application.ScreenUpdating = False
Filepath = "\\NAMDFS\TPA\MWD\USERS\ay86009\Referral_Doc\"
MyFile = Dir(Filepath)
Do While Len(MyFile) > 0
If MyFile = "Referral_Doc_Collation.xlsm" Then
Exit Sub
End If
Workbooks.Open (Filepath & MyFile)
Sheets("Allocation").Range("B2:L3000").Copy
Application.DisplayAlerts = False
erow = Sheet1.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Allocation").Range(Cells(erow, 2), Cells(erow, 12))
activesheet.next.select
Sheets("Prefetcher").Range("B2:I3000").Copy
Application.DisplayAlerts = False
erow = Sheet2.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Prefetcher").Range(Cells(erow, 2), Cells(erow, 9))
activesheet.next.select
Sheets("Matrix").Range("B2:G3000").Copy
Application.DisplayAlerts = False
erow = Sheet3.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Matrix").Range(Cells(erow, 2), Cells(erow, 7))
activesheet.next.select
Sheets("Follow ups").Range("B2:H3000").Copy
Application.DisplayAlerts = False
erow = Sheet4.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Follow ups").Range(Cells(erow, 2), Cells(erow, 8))
ActiveWorkbook.Close
MyFile = Dir
Loop
Application.ScreenUpdating = True
MsgBox "DONE"
End Sub
Compiled but not tested: 编译但未经测试:
Sub Ref_Doc_Collation()
Const FILE_PATH As String = "\\NAMDFS\TPA\MWD\USERS\ay86009\Referral_Doc\"
Const SKIP_FILE As String = "Referral_Doc_Collation.xlsm"
Dim MyFile As String, wb As Workbook
Application.ScreenUpdating = False
MyFile = Dir(FILE_PATH)
Do While Len(MyFile) > 0
If MyFile <> SKIP_FILE Then
Set wb = Workbooks.Open(FILE_PATH & MyFile)
wb.Sheets("Allocation").Range("B2:L3000").Copy _
ThisWorkbook.Sheets("Allocation").Cells(Rows.Count, "B"). _
End(xlUp).Offset(1, 0)
wb.Sheets("Prefetcher").Range("B2:I3000").Copy _
ThisWorkbook.Sheets("Prefetcher").Cells(Rows.Count, "B"). _
End(xlUp).Offset(1, 0)
wb.Sheets("Matrix").Range("B2:G3000").Copy _
ThisWorkbook.Sheets("Matrix").Cells(Rows.Count, "B"). _
End(xlUp).Offset(1, 0)
wb.Sheets("Follow ups").Range("B2:H3000").Copy _
ThisWorkbook.Sheets("Allocation").Cells(Rows.Count, "B"). _
End(xlUp).Offset(1, 0)
wb.Close False
End If
MyFile = Dir
Loop
Application.ScreenUpdating = True
MsgBox "DONE"
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.