[英]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
.我需要从数据复制Sheet1
的Workbook1
到Sheet2
的Workbook2
。 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) 并将整个复制的数据粘贴到Workbook2
的Sheet2
的Q1
。
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.