[英]VBA to copy a range from multiple workbooks in a folder into another folder
I have 10 excel files in Folder A. I also have other 10 excel files in Folder B. The 10 files in both folder have the same name.我在文件夹 A 中有 10 个 excel 文件。我在文件夹 B 中还有其他 10 个 excel 文件。两个文件夹中的 10 个文件具有相同的名称。 I am trying to copy range A2:B20 of the active worksheet from each of those 10 excel files in Folder A into the other 10 corresponding excel files in Folder B. All files in Folder B only have 1 worksheet named Sheet0.
我正在尝试将活动工作表的范围 A2:B20 从文件夹 A 中的这 10 个 excel 文件中的每一个复制到文件夹 B 中其他 10 个相应的 excel 文件中。文件夹 B 中的所有文件只有一个名为 Sheet0 的工作表。 I want to have the range A2:B20 at the end of column A and B of Sheet0 in every excel files in folder B.
我想在文件夹 B 中的每个 excel 文件中的 Sheet0 的 A 和 B 列的末尾都有范围 A2:B20。
Below is my code.下面是我的代码。 I have tried multiple times but it did not work
我已经尝试了多次,但没有奏效
Sub Copy_range()
Const FolderPath1 = "C:\Users\***\Documents\Folder A\"
Const FolderPath2 = "C:\Users\***\Documents\Folder B\"
Dim Filename1 As String: Filename1 = Dir(FolderPath1 & "*.csv")
Dim Filename2 As String: Filename2 = Dir(FolderPath2 & "*.xlsx")
Dim dws As Worksheet: Set dws = Workbooks(Filename2).Worksheets("Sheet0")
Dim dCell As Range: Set dCell = dws.Cells(dws.Rows.Count, "A1:B").End(xlUp)
Application.ScreenUpdating = False
Do While Filename1 <> ""
Set dCell = dCell.Offset(1)
With Workbooks.Open(Filename1:=FolderPath1 & Filename1, ReadOnly:=True)
dCell.Value = .ActiveSheet.Range("A2:B20").Value
.Close False
End With
Filename1 = Dir()
Loop
Application.ScreenUpdating = True
End Sub
You forgot to open the workbook before trying to set dws to the sheet.在尝试将 dws 设置为工作表之前,您忘记打开工作簿。 Also, your expression to set dCell would cause an error due to "A1:B" not being a valid column input.
此外,由于“A1:B”不是有效的列输入,您设置 dCell 的表达式会导致错误。 Finally, the
.Offset(1)
of dCell will only work on the first iteration.最后,dCell 的
.Offset(1)
将仅在第一次迭代时起作用。 Afterwards, you will want to .Offset(19)
because you have pasted in 19 new rows.之后,您将需要
.Offset(19)
,因为您已粘贴了 19 个新行。 I have corrected those three issues in the following code:我在以下代码中更正了这三个问题:
Sub Copy_range()
Const FolderPath1 = "C:\Users\***\Documents\Folder A\"
Const FolderPath2 = "C:\Users\***\Documents\Folder B\"
Dim Filename1 As String: Filename1 = Dir(FolderPath1 & "*.csv")
Dim Filename2 As String: Filename2 = Dir(FolderPath2 & "*.xlsx")
Dim dws As Worksheet
Dim dCell As Range
Set dws = Application.Workbooks.Open(FolderPath2 & Filename2).Worksheets("Sheet0")
Set dCell = dws.Cells(dws.Rows.Count, 1).End(xlUp).Offset(1)
Application.ScreenUpdating = False
Do While Filename1 <> ""
With Workbooks.Open(FolderPath1 & Filename1, ReadOnly:=True)
dCell.Value = .ActiveSheet.Range("A2:B20").Value
.Close False
End With
Filename1 = Dir()
Set dCell = dCell.Offset(19)
Loop
Application.ScreenUpdating = True
End Sub
Truthfully, Offset
is not the best way to advance the output range because it can potentially leave a lot of blank rows in your data.说实话,
Offset
量并不是推进 output 范围的最佳方式,因为它可能会在您的数据中留下大量空白行。 It would be better to just re-set dCell with dws.Cells(dws.Rows.Count, 1).End(xlUp).Offset(1)
最好用
dws.Cells(dws.Rows.Count, 1).End(xlUp).Offset(1)
重新设置 dCell
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.