将粘贴复制到两个不同工作簿之间的错误单元格范围内

时间:2016-05-27 10:10:06

标签: excel vba excel-vba

我已经管理并将粘贴数据从一个工作簿复制到主工作簿,但是,数据没有粘贴到正确的位置。数据将在源工作簿的范围(" A1:C12")内复制,但会粘贴在主工作簿中的范围" M75:O86")中。我希望它可以粘贴在主工作簿中的Range(" A1:C12")中。请帮忙。

   Option Explicit
   Sub Copydata()
   Dim myWB As Workbook
   Dim thisWB As Workbook
   Dim thisWS As Worksheet

   Set myWB = Workbooks.Open("C:\Users\jjordan\Desktop\test.xlsx")

   myWB.Sheets("Sheet1").Range("A1:C12").Select
   Selection.Copy

   Set thisWB = ThisWorkbook
   Set thisWS = ThisWorkbook.Sheets("Test data")

   thisWB.Activate   
   thisWS.Select

  Selection.Range("A1").PasteSpecial Paste:=xlPasteValues,  Operation:=xlNone,SkipBlanks _
   :=False, Transpose:=False
   Application.CutCopyMode = False
   Application.ScreenUpdating = True

   myWB.Close

   End Sub

1 个答案:

答案 0 :(得分:1)

Select语句很危险并且会大大减慢您的代码速度。尝试使用此

Option Explicit
Sub Copydata()
    Dim myWB As Workbook
    Dim thisWB As Workbook
    Dim thisWS As Worksheet

    Set myWB = Workbooks.Open("C:\Users\jjordan\Desktop\test.xlsx")

    myWB.Sheets("Sheet1").Range("A1:C12").Copy

    Set thisWB = ThisWorkbook
    Set thisWS = thisWB.Sheets("Test data")

    thisWB.Activate
    thisWS.Range("A1").PasteSpecial _
        Paste:=xlPasteValues, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False

    With Application
        .CutCopyMode = False
        .ScreenUpdating = True
    End With

    myWB.Close

End Sub