我不知道如何在不同的工作簿之间复制单元格值(即使是合并的单元格)而不使用。activate。我知道如何避免选择;Dim x为range将wrk1指定为工作簿将wr2指定为工作簿昏暗的w波段昏暗的范围
,但我不能复制范围没有使用。激活…我想让一些东西像这样工作:
sub avoid()
dim x as range
dim wrk1 as workbook
dim wrk2 as workbook
dim w as range
dim r as range
dim e as range
set wrk1 = application.workbook("cartel1") 'already open workbook
set wrk2 = workbook.open("workB") 'workbook was closed
set x = wrk1.range("a1")
set e = wrk1.range("a3")
set w = wrk2.range("a1")
set r = wrk2.range("a2")
x.copy(w)
e.copy(r)
end sub
但它不起作用。任何帮助吗?由于
你犯了很多错误。所以,我修改了它。我已经测试了代码。它对我很有用。试试这个:
Public Sub avoid()
Dim source1 As Range
Dim source2 As Range
Dim dest1 As Range
Dim dest2 As Range
Dim sourceBook As Workbook
Dim destBook As Workbook
Set sourceBook = Application.Workbooks("cartel1")
'Here you need to set full path of new work book
'Because new work book is same level with source work book, so I set => ThisWorkbook.Path & "" & "workB"
'Open destination work book
Workbooks.Open Filename:=ThisWorkbook.Path & "" & "workB"
Set destBook = Application.Workbooks("workB")
'Here, we can't get a Range from a workbook.
'Only, we can get it from a worksheet. You forget to get sheet. I do it. Check.
'You need to modify sheet names.
Set source1 = sourceBook.Sheets("sourceSheet").Range("A1")
Set source2 = sourceBook.Sheets("sourceSheet").Range("A3")
Set dest1 = destBook.Sheets("destSheet").Range("A1")
Set dest2 = destBook.Sheets("destSheet").Range("A2")
source1.Copy dest1
source2.Copy dest2
End Sub
去掉括号:
x.copy w
e.copy r
或者只是复制值:
w.Value = x.Value
r.Value = e.Value