我目前正在尝试从一个工作表中复制并使用VBA粘贴到新工作表中。但是我有以下变量
我目前设法提出以下代码,但我不断收到错误,并且因为我是VBA的新手而不知道我哪里出错了。
Sub CandP()
'
'
Dim Last_Row As Long
Application.ScreenUpdating = False
Last_Row = Range("A2:BC2" & Rows.Count).End(xlUp).Row
Range("A2:BC2").Copy
Windows("Newsheet.xlsm").Activate
Range("$A2:BC$" & last_row).FillDown
End Sub
感谢所有帮助,谢谢
答案 0 :(得分:3)
你可以试试这个:
Option Explicit
Sub CandP()
Dim Last_Row1 As Long, Last_Row2 As Long
Dim WB1 As Workbook, WB2 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Set WB1 = ThisWorkbook ' Workbook where you want to copy the data
Set ws1 = WB1.Sheets("Sheet1") ' Change the name of your Sheet
Set WB2 = Workbooks.Open("C:\Desktop\vba\Newsheet.xlsm") ' Enter the address of the Workbook you want to paste the data
Set ws2 = WB2.Sheets("Sheet1") ' Change the name of your Sheet
Last_Row1 = ws1.Range("A" & Rows.Count).End(xlUp).Row ' Determine the lastrow of the data to copy
Last_Row2 = ws2.Range("A" & Rows.Count).End(xlUp).Row + 1 ' Determine the next empty row in order to paste the data
ws1.Range("A2:BC" & Last_Row1).Copy ws2.Range("A" & Last_Row2)
End Sub