简体   繁体   English

如何将数据从工作簿 1 的工作表 1 复制到工作簿 2 的工作表 2?

[英]How to copy data from sheet1 of workbook1 to sheet 2 of Workbook2?

I need to copy Data from Sheet1 of Workbook1 to Sheet2 of Workbook2 .我需要从数据复制Sheet1Workbook1Sheet2Workbook2 I need to copy data of sheet 1 from A1 to AE (LastRow) and paste the whole copied data to Q1 of Sheet2 of Workbook2 .我需要将工作表 1 的数据从A1复制到AE (LastRow) 并将整个复制的数据粘贴到Workbook2Sheet2Q1

Here is my code:这是我的代码:

Set wbkA = Workbooks.Open(Filename:="D:\Excel\Workbook1.xlsx")

     For Each oSheet In wbkA.Sheets

        If oSheet.Name = "Sheet1" Then
           Set NewWorksheetA = wbkA.Worksheets(oSheet.Name)
        End If

    Next oSheet
Set wbkB = Workbooks.Open(Filename:="D:\Excel\Workbook2.xlsx")

     For Each oSheet In wbkB.Sheets

        If oSheet.Name = "Sheet2" Then
           Set NewWorksheetB = wbkB.Worksheets(oSheet.Name)
        End If

    Next oSheet
''''''''''''''''''''''''''''''''''''''''''''''''''''''' for Loop for row and Column Starts here to compare the data
If Not NewWorksheetA Is Nothing Then

''''''''''''''''''''''''''''''''''''''''''''''''''''''' Getting Last Column index from current Worbook to Write the UpdateStatus
LastRow = NewWorksheetA.Cells(NewWorksheetA.Rows.Count, 2).End(xlUp).Row
'MsgBox "Last Row Number:" & LastRow
LastColumn = NewWorksheetA.Cells(8, NewWorksheetA.Columns.Count).End(xlToLeft).Column
'MsgBox "Last Column Number:" & LastColumn

Workbooks("D:\Excel\Workbook1.xlsx").Worksheets("Sheet1").Range("A1:AE" & LastRow).Copy _
Workbooks("D:\Excel\Workbook2.xlsx").Worksheets("Sheet2").Range("Q1")

End If

I am getting the below error我收到以下错误

Run Time Error 9: Subscript out of range.

I would like to use the variable used for Workbook and sheet name我想使用用于工作簿和工作表名称的变量

Replace this code:替换此代码:

Workbooks("D:\Excel\Workbook1.xlsx").Worksheets("Sheet1").Range("A1:AE" & LastRow).Copy _
Workbooks("D:\Excel\Workbook2.xlsx").Worksheets("Sheet2").Range("Q1")

with this:有了这个:

NewWorksheetA.Range("A1:AE" & LastRow).Copy
NewWorksheetB.Range("Q1").PasteSpecial xlPasteAll
Application.CutCopyMode = False

Hope my answer helps.希望我的回答有帮助。 :) :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从一个已经打开的Excel文件(Workbook1,Sheet1,单元格A11)复制到另一个已经打开的Excel文件(workbook2,sheet1,A11) - How to copy data from one already opened excel file (Workbook1,Sheet1,cell A11) to another already opened excel file(workbook2,sheet1,A11) 如何在workbook1更新时将workbook1中的数据添加到workbook2和workbook2更新中? - How to add data from workbook1 into workbook2 and workbook2 updates when workbook1 updates? 从Workbook1中对Workbook2中的数据进行排序 - Sorting Data in Workbook2 from Workbook1 如何将Workbook1 / Sheet3中的选定行与Workbook2 / Sheet3中的选定行进行比较 - How to compare a selected row in Workbook1/Sheet3 with a selected row in Workbook2/Sheet3 从 Workbook1、Column1 中查找与 Workbook2、Column 1 中的值不同的值并在新工作表中显示 - Find Values from Workbook1, Column1 that are unique from values in Workbook2, Column 1 and display in new sheet 循环搜索workbook1中的数据,将偏移单元格复制到workbook2 - Loop to search data in workbook1, copy offset cell to workbook2 EPPlus 将工作表从 Workbook1 复制到 Workbook2 - EPPlus To Copy Worksheet From Workbook1 to Workbook2 在工作簿2中找到一个值,并将偏移值复制到工作簿1中 - Find a values in workbook2 and copy offset value to workbook1 将复制工作表1更改为在宏中复制工作簿 - Change Copy Sheet1 to Copy Workbook in Macro VBA将数据从一个工作簿表复制到另一工作簿表 - VBA copy data from one workbook sheet to another workbook sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM