简体   繁体   English

VBA 将一个文件夹中多个工作簿的范围复制到另一个文件夹

[英]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.

相关问题 将范围从文件夹中的多个工作簿复制到文件夹中的摘要工作簿吗? - Copy Range from multiple workbooks in folder to Summary Workbook also in folder? VBA将工作表从一个工作簿复制到另一个文件夹中的所有工作簿 - VBA to copy worksheet from one workbook to all workbooks in another folder VBA将多个工作簿中的粘贴复制到同一文件夹中的1个中 - VBA copy Paste from multiple workbooks into 1 on same folder VBA从文件夹中的工作簿复制单元格值 - VBA to copy cell values from workbooks in a folder 如何创建 VBA 宏,它将数据从文件夹中的多个源工作簿复制到另一个工作簿,然后另存为新工作簿 - How to create a VBA macro that will copy data from multiple source workbooks within a folder to another workbook thereafter saving as a new workbook Excel VBA:在一个文件夹中的多个工作簿上循环工作表的简单副本 - Excel VBA : Looping a simple copy of a worksheet over multiple workbooks in a folder 从同一文件夹复制工作簿之间的单元格范围 - Copy cell range between workbooks from same folder VBA-合并文件夹和SUBFOLDER中所有工作簿的范围 - VBA - Merging a Range from All Workbooks in a Folder and SUBFOLDER VBA-将多个选定文件从一个文件夹复制到另一个 - VBA - Copy multiple selected files from 1 folder to another VBA:将特定范围从多个工作簿复制到一个工作表中 - VBA: Copy specific range from multiple workbooks into one worksheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM