[英]VBA Copy Data from several Different workbook in a specific worksheet and then paste the data to a specific sheet in current Workbook
I am very new to VBA and need some help with copy and paste with different workbook and loop sheet names. 我是VBA的新手,需要一些帮助来复制和粘贴不同的工作簿和循环表名称。
I need to write a Macro to copy data from 100 workbooks (with exactly same formatting and sheet names), need copy from two sheets named "Summary" and "DB", to my current workbook's different sheets. 我需要编写一个宏来复制100个工作簿中的数据(具有完全相同的格式和工作表名称),需要从两个名为“Summary”和“DB”的工作表复制到我当前工作簿的不同工作表。 I have set up a Control_Panel Sheet in my current workbook with the name of the copy source files' name and I need to escape the ones are blank (Or delete the sheet that does not have the copy source file, that's even better) 我在我当前的工作簿中设置了一个Control_Panel Sheet,其中包含复制源文件名称的名称,我需要将其转义为空白(或者删除没有复制源文件的表单,这样更好)
The Control_Panel Sheet Page looks like this: Control_Panel工作表页面如下所示:
Here is my Code: 这是我的代码:
Option Explicit
Sub CopyData()
Dim Address As String
Dim CopyAddress As String
Dim CopyRef1 As String
Dim CopyRef2 As String
Dim PasteSheet As String
Dim Sheet1 As String
Dim Sheet2 As String
Dim A As Worksheet
Dim i As Integer
For i = 8 To 107 Step 1
If IsEmpty(Cells(i, "D").Value) = False Then
Sheet1 = "Summary"
Sheet2 = "DB"
Address = Cells(i, "D").Value
PasteSheet = Cells(i, "B").Value
CopyRef1 = "'" & Cells(4, "C") & "[& Address &]" & Sheet1 & "'!'" & Range("B6:DO20")
A = Worksheets("Sheet" & PasteSheet).Activate
A.Range("C6:DP20").PasteSpecial
CopyRef2 = "'" & Cells(4, "C") & "[& Address &]" & Sheet2 & "'!'" & Range("B7:LK631").Copy
ActiveWorkbook.Worksheets(PasteSheet).Range("B23:LK647").PasteSpecial
End If
Next i
End Sub
I do not want to open the copy source file each time when I try to copy the data, and it seems that the code does not recognize the location of the source file. 我每次尝试复制数据时都不想打开复制源文件,而且代码似乎无法识别源文件的位置。 Also, I am not sure if "PasteSheet" name is recognized when I am trying to paste data to each of the sheet. 此外,当我尝试将数据粘贴到每个工作表时,我不确定是否识别“PasteSheet”名称。
Please help me to find a way to copy the data from two sheets, named "Summary" and "DB", from multiple workbooks without open them and paste the data to different sheet with the sheet name loops from Cell B8 to B107. 请帮我找到一种方法,从多张工作簿中复制两张名为“Summary”和“DB”的数据,而不打开它们,并将数据粘贴到不同的工作表,工作表名称从Cell B8循环到B107。
Thank you so much! 非常感谢!
It is very difficult to see what you are trying. 很难看出你在尝试什么。 Try to name your workbooks and worksheets like so: 尝试将工作簿和工作表命名为:
Dim wb as workbook, dim ws as worksheet,
Set wb = ThisWorkbook,
Set ws = wb.Worksheets("thenameofyourworksheet")
And then work with ws.cells(4, "C")
etc. At least one error I can see in your code: 然后使用ws.cells(4, "C")
等。我可以在您的代码中看到至少一个错误:
Your both variables CopyRef1/2
contain the Address
term, which is(should be) in fact a variable. 您的两个变量CopyRef1/2
都包含Address
term,它实际上是(应该)变量。 So the placing of your " "
is incorrect or insufficient. 因此,放置您的" "
是不正确的或不充分的。
Try ... & "[" & Address & "]"
试试... & "[" & Address & "]"
Pro Tip: Use flags! 专业提示:使用标志! Do not name variables like Address
or so, name it sAddress
and flag your type. 不要名称,如变量Address
左右,将其命名sAddress
和标志你的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.