Excel VBA, getting range from an inactive sheet

前端 未结 3 2159
走了就别回头了
走了就别回头了 2020-11-21 06:29

This script works fine when I\'m viewing the \"Temp\" sheet. But when I\'m in another sheet then the copy command fails.. It gives a \"Application-defined or object-defined

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-21 06:41

    This will do, I don't like to use (xlDown) in case a cell is empty.

    Dim lRow As Long
    lRow = Sheets("Temp").Cells(Cells.Rows.Count, "A").End(xlUp).Row
    
    With Sheets("Temp")
         .Range("A1:A" & lRow).Copy Sheets("Overview").Range("C40")
    End With
    

    Or if you want to just use Columns...

    Sheets("Temp").Columns(1).SpecialCells(xlCellTypeConstants).Copy Destination:=Sheets("Overview").Range("C40")
    

提交回复
热议问题