首页 > 解决方案 > 从打开的工作簿的指定工作表复制数据并将其粘贴到另一个指定的关闭工作簿工作表

问题描述

我想将数据从打开的工作簿中的指定工作表复制到关闭的工作簿中的另一个指定工作表。
我有这样的代码:

Private Sub CommandButton1_Click()

    Dim wsCopy As Worksheet
    Dim wsDest As Worksheet
    Dim lCopyLastRow As Long
    Dim lDestLastRow As Long

    'Set variables for copy and destination sheets
    Set wsCopy = Workbooks("Form Marketing Calendar1.xlsm").Worksheets("Form Single")
    Set wsDest = Workbooks.Open("Database Marketing Calendar.xlsx").Worksheets("Sheet1")

    '2. Find first blank row in the destination range based on data in column A
    'Offset property moves down 1 row
    lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1).Row

    '3. Copy & Paste Data
    wsCopy.Range("B22:AF25").Copy
    wsDest.Range("B" & lDestLastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=True
    wsCopy.Range("B26:AF30").Copy
    wsDest.Range("G" & lDestLastRow).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=True
    Selection.ClearContents
    wsDest.Parent.Close True

End Sub

实际上,它复制了数据,但它必须先打开目标文件然后自动关闭它。

它也只复制第一个数据(this: wsCopy.Range("B22:AF25").Copy),但第二个数据没有复制(that: wsCopy.Range("B26:AF30").Copy)。

标签: excelvbaexcel-2016

解决方案


这里:

Option Explicit
Private Sub CommandButton1_Click()

    Dim wsCopy As Worksheet
    Dim wsDest As Worksheet
    Dim lCopyLastRow As Long
    Dim lDestLastRowB As Long
    Dim lDestLastRowG As Long

    'It is not possible to get data from a workbook without openning it, but this is the closest
    With Application
        .ScreenUpdating = False 'won't show any changes on your screen
        .Visible = Flase 'will hide Excel. CAUTION: if the macro fails make sure to change this back to True
    End With

    'Set variables for copy and destination sheets
    Set wsCopy = Workbooks("Form Marketing Calendar1.xlsm").Worksheets("Form Single")
    Set wsDest = Workbooks.Open("Database Marketing Calendar.xlsx").Worksheets("Sheet1")

    '2. Find first blank row in the destination range based on data in column A
    'Offset property moves down 1 row
    With wsDest 'with allows you to refer to the variable without writting it
        lDestLastRowB = .Cells(.Rows.Count, "B").End(xlUp).Row + 1 'You can use also +1 since it's a number
        'you are not copying the same amount of rows, so maybe the data won't be on the same row on both columns
        lDestLastRowG = .Cells(.Rows.Count, "G").End(xlUp).Row + 1
    End With

    '3. Copy & Paste Data
    wsCopy.Range("B22:AF25").Copy
    wsDest.Range("B" & lDestLastRowB).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=True
    wsCopy.Range("B26:AF30").Copy
    wsDest.Range("G" & lDestLastRowG).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=True
    'when you manually copy and paste you will see that the paste range gets selected. This line erases selected content so this is deleting it
    'Selection.ClearContents
    wsDest.Parent.Close True

    With Application
        .ScreenUpdating = True
        .Visible = True
    End With

End Sub

推荐阅读