复制不同工作簿之间的粘贴

时间:2018-01-05 18:49:47

标签: vba excel-vba copy-paste excel

您好我想将范围复制并粘贴到mainworkbook,但是,范围会粘贴在babyworkook8上。请帮忙

Option Explicit
Dim mainworkbook As Workbook
Dim babyworkbook8 As Workbook


Sub Copypaste()
Set babyworkbook8 = Workbooks.Open("\\C:\IT Charges\IT Charges August.xlsx")

babyworkbook8.Sheets("Sheet1").Range("D4:F27").Copy
Set mainworkbook = Workbooks.Open("\\C:\Users\ivan\Desktop\IT Charges\IT 
Summary Charges.xlsm")


mainworkbook.Sheets("Sheet1").Range("R1").PasteSpecial


End Sub

1 个答案:

答案 0 :(得分:0)

这有点长,但它清楚地定义了源和目标范围:

Sub CopyPaste()
    Dim SourceBook As Workbook
    Dim DestBook As Workbook
    Dim SourceSheet As Worksheet
    Dim DestSheet As Worksheet
    Dim SourceRange As Range
    Dim DestRange As Range

    Set SourceBook = Workbooks.Open("\\C:\IT Charges\IT Charges August.xlsx")
    Set DestBook = Workbooks.Open("\\C:\Users\ivan\Desktop\IT Charges\IT Summary Charges.xlsm")
    Set SourceSheet = SourceBook.Sheets("Sheet1")
    Set DestSheet = DestBook.Sheets("Sheet1")
    Set SourceRange = SourceSheet.Range("D4:F27")
    Set DestRange = DestSheet.Range("R1")

    SourceRange.Copy DestRange

End Sub