将外部工作簿XLDOWN复制到当前工作簿

时间:2016-11-30 22:08:24

标签: excel vba excel-vba

我正在尝试复制来自单元格的所有结果" A9"使用XLDOWN从名为" DDR"的文件中告诉数据的结尾&安培;标签为" everett"并将其粘贴回我当前使用的工作簿中。

非常感谢任何帮助

Sub XLDOWN1()
'
' XLDOWN1 Macro
'

'
    Dim wbSource As Workbook, wbDest As Workbook
    Dim wsSource As Worksheet, wsDest As Worksheet
    Dim rngSource As Range, rngDest As Range

    Set wbSource = Workbooks.Open("G:\GAGC\Accounting\Payroll\Payroll\Analysis Macro Upload\DDR.xlsx", , True)
    Set wsSource = wbSource.Worksheets("Everett")


    ws.Range("A9", Range("A9").End(xlDown)).Select
    Selection.Copy



    Set rngSource = wsSource.Range("A9").Range(Selection, Selection.End(xlDown))
    Set wbDest = ThisWorkbook
    Set wsDest = wbDest.Worksheets("2016")
    Set rngDest = wsDest.Range("A4") 'Destination Cell

    rngDest.Value = rngSource.Value 'Copies values over only

    wbSource.Close (False) 'Close without saving changes


End Sub

1 个答案:

答案 0 :(得分:0)

给它一个机会。您提供的代码中存在一些问题,在看到下面重构的代码后应该清楚。

Dim wbSource As Workbook, wbDest As Workbook
Dim wsSource As Worksheet, wsDest As Worksheet
Dim rngSource As Range, rngDest As Range

'set up source workbook, sheet, range
Set wbSource = Workbooks.Open("G:\GAGC\Accounting\Payroll\Payroll\Analysis Macro Upload\DDR.xlsx", , True)
Set wsSource = wbSource.Worksheets("Everett")
Set rngSource = wsSource.Range(wsSource.Range("A9"), wsSource.Range("A9").End(xlDown))

'set up destination workbook, sheet, range
Set wbDest = ThisWorkbook
Set wsDest = wbDest.Worksheets("2016")
Set rngDest = wsDest.Range("A4") 'Destination Cell

rngSource.Copy Destination:=rngDest

wbSource.Close False