复制范围结束xl向下粘贴不同的Wb

时间:2017-08-04 08:33:10

标签: vba variables copy paste

我有一个宏记录器代码(带选择和激活),我正在努力简化。它目前看起来像这样:

Windows("Stambestand.xlsm").Activate
Range("AA2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
    Windows("Ijking document.xlsm").Activate
    Range("B3").Select
    ActiveSheet.Paste

最近我开始使用变量并希望减少代码。我正在考虑这些问题:

WbStambestand.WsStam.Range("AA2", Selection, Selection.End(xlDown)).Copy WbIjk.WsIjk.range("A1").paste

Workbooks("WbStambestand").Worksheets("WsStam").Range("AA2", Selection, Selection.End(xlDown)).Copy

然而,这些不起作用。我希望你们能帮助我。非常感谢。

仅供参考,这些是我的变量(它们被声明)。 Ijk是粘贴目的地。

Dim WbStambestand, WbIjk As Workbook 
Dim WsIjk, WsStam As Worksheet

Set WsIjk = ActiveSheet 
Set WbIjk = ActiveWorkbook
Set WsIjk = ActiveSheet
Set WbIjk = ActiveWorkbook
Set WbStambestand = Workbooks.Open(stam)
Set WsStam = WbStambestand.Worksheets("stambestand")

1 个答案:

答案 0 :(得分:0)

试试这个

    Sub abc()
    Dim WbStambestand As Workbook
    Dim WbIjk As Workbook
    Dim WsIjk As Worksheet
    Dim WsStam As Worksheet
    Dim LastRow As Long
    Dim stam As String

    stam = "C:\Users\Admin\Desktop\Stambestand.xlsm" ' path with complete file name with extension.
    Set WsIjk = ActiveSheet
    Set WbIjk = ThisWorkbook ' workbook which has current code
    Set WbStambestand = Workbooks.Open(stam)
    Set WsStam = WbStambestand.Worksheets("stambestand")

    LastRow = WsStam.Range("AA2").End(xlDown).Row
    WsStam.Range("AA2:AA" & LastRow).Copy WsIjk.Range("B3")
    End Sub