对象定义的错误 - 复制范围直到最后一行信息

时间:2017-07-05 20:48:02

标签: vba excel-vba excel

我正在尝试打开工作簿,然后选择并将范围从A7复制到L7,直到最后一行信息(向下)。我认为变量没有正确定义。有帮助吗?

Sub Subtotals()
Dim DT As Workbook
Dim lRowDT As Integer


Set DT = Workbooks.Open("C:\Users\ricardo\Desktop\Data Fuel.xls")
lRowDT = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

Workbooks("Data Fuel.xls").Worksheets("Data").Activate
Worksheets("Data").Range("A7:L" & lRowDT).Copy

End sub

1 个答案:

答案 0 :(得分:0)

如果没有colums L上的数据,那么此代码是合适的。

Sub Subtotals()
Dim DT As Workbook
Dim lRowDT As Integer
Dim Ws As Worksheet
Dim rngDB As Range, r As Long, c As Long

    Set DT = Workbooks.Open("C:\Users\ricardo\Desktop\Data Fuel.xls")
    'lRowDT = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

    Set Ws = Workbooks("Data Fuel.xls").Worksheets("Data")
    With Ws
        r = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        c = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
        Set rngDB = .Range("a7", .Cells(r, "L"))
    End With
    rngDB.Copy
End Sub