简体   繁体   中英

Excel VBA copy cells between two workbooks

I'm trying to copy data between two workbooks: from cell A3 to the last cell in Voice to the corresponding cells in Voice_Files. I'm having trouble getting it to work, what am I doing worng? Below is the code:

Sub Copy()
    Dim wB1 As Workbook
    Dim wB2 As Workbook
    Dim wS1 As Worksheet
    Dim wS2 As Worksheet
    Dim c1 As Range
    Dim c2 As Range


    Dim MASTER_FILE_NAME As String
    MASTER_FILE_NAME = "Voice.xlsm"
    Dim REPORT_FILE_NAME As String
    REPORT_FILE_NAME = "Voice_Files.xlsm"


    'Set wB1 = ThisWorkbook
    'Set wB1 = ActiveWorkbook
    Set wB1 = Workbooks("Voice.xlsx")
    Set wB2 = Workbooks("Voice_Files.xlsx")

    Set wS1 = wB1.Sheets(1)
    Set wS2 = wB2.Sheets(1)


    'Take the whole column in master sheet
    Set c1 = wS1.Range("A2").End(xlDown).End(xlToRight)
    Set c2 = wS2.Range("A2").End(xlDown).End(xlToRight)

    'Transfer the values
    c2.Value = c1.Value
End Sub

You can enclose Range s within another Range call, ie 1st is the top left cell and 2nd is the bottom right.

With ws1
    Set c1 = .Range(.Range("A2"), .Range("A2").End(xlDown).End(xlToRight))
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM