有一个简单的任务我无法弄清楚。我想复制wb1.ws1的值并粘贴到wb2.ws2
这是我尝试过的。
Dim wb1 as Workbook, wb2 as Workbook
Dim ws1 as Worksheet, ws2 as Worksheet
'Open wb1 and ws1 and set
Set wb1 = Workbooks.Open("R:\Workbook1.xlsx")
Set ws1 = Sheet("Sheet1")
'Open wb2 and ws2 and set
Set wb2 = Workbooks.Open("R:\Workbook2.xlsx")
Set ws2 = Sheet("Sheet2")
'Copy Value of wb1.ws1 and paste into wb2.ws2
wb1.ws1.Range("A1").Copy wb2.ws2.Range("A1")
为什么这不起作用?看来我无法通过指定wb1.ws1.Range(XYZ)
选择范围这迫使我将行分隔成
wb1.Activate
ws1.Select
Range("A1").Copy
wb2.Activate
ws2.Select
Range("A1").Paste
似乎效率很低,但我不知道如何消除“激活选择”梦night
答案 0 :(得分:1)
一旦设置了工作表,便已经定义了工作簿父级。因此请在Set
Dim wb1 as Workbook, wb2 as Workbook
Dim ws1 as Worksheet, ws2 as Worksheet
'Open wb1 and ws1 and set
Set wb1 = Workbooks.Open("R:\Workbook1.xlsx")
Set ws1 = wb1.WorkSheets("Sheet1")
'Open wb2 and ws2 and set
Set wb2 = Workbooks.Open("R:\Workbook2.xlsx")
Set ws2 = wb2.Worksheets("Sheet2")
'Copy Value of wb1.ws1 and paste into wb2.ws2
ws1.Range("A1").Copy ws2.Range("A1")