两个工作簿之间的Excel VBA复制单元格

时间:2019-10-24 20:38:44

标签: excel vba

我正在尝试在两个工作簿之间复制数据:从单元格A3到Voice中的最后一个单元格,再到Voice_Files中的相应单元格。我无法正常工作,我在做什么?下面是代码:

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

1 个答案:

答案 0 :(得分:3)

您可以将Range括在另一个Range调用中,即第1个是左上角的单元格,第2个是右下角的单元格。

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