我想将两个excel的内容复制到一个中,一切顺利,直到我复制第二个excel的内容,因为这会覆盖第一个复制excel的内容,完成了:
这些是我的陈述:
Dim wbOrigen1 As Workbook, _
wbOrigen2 As Workbook, _
wsDestino As Excel.Worksheet, _
wsOrigen1 As Excel.Worksheet, _
wsOrigen2 As Excel.Worksheet, _
rngOrigen1 As Excel.Range, _
rngDestino As Excel.Range, _
rngDestino2 As Excel.Range, _
rngOrigen2 As Excel.Range
这是问题
ThisWorkbook.Activate
Set rngDestino2 = wsDestino.Range(celdaDestino,Range(celdaDestino).End(xlDown).Offset(1, 0))
Range(celdaDestino).End(xlDown).Offset(1, 0).Select
wsOrigen2.Activate
rngOrigen2.Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
ThisWorkbook.Activate
rngDestino2.PasteSpecial xlPasteValues
Application.CutCopyMode = False
但这不起作用。问题似乎与rngDestino2
?rngDestino2
Type Mismatch
?err.Description
Type Mismatch
我该如何解决?
答案 0 :(得分:1)
我更愿意避免选择,激活和选择直接寻址。通过直接单元格值传输,也可以更有效地处理粘贴特殊值。
'make sure that the worksheet vars are set correctly
set wsOrigen2 = <other workbook>.Sheets("Sheet1")
set wsDestino = ThisWorkbook.Sheets("Sheet1")
With wsOrigen2.Cells(1, 1).CurrentRegion
wsDestino.Cells(Rows.Count, 1).End(xlUp) _
.Offset(1, 0).Resize(.Rows.Count, .Columns.Count) = .Cells.Value
End With
set wsDestino = nothing
set wsOrigen2 = nothing
如果工作簿已打开并且工作表设置正确,那么这应该是您真正需要的。
有关远离选择和激活回复的更多方法,请参阅How to avoid using Select in Excel VBA macros。