简体   繁体   中英

ActiveSheet.Range(Cells(x,y), Cells(w, z)).Select not working

Could anyone tell me why when I refer to a particular range it works fine:

ActiveSheet.Range("A1:D3").Select

but

ActiveSheet.Range(Cells(1, 1), Cells(3, 4)).Select

not working?

I suspect your code is in the worksheet code module of a different sheet, so the unqualified Cells calls refer to that sheet, not the active one. You should always qualify all Range or Cells calls with a Worksheet object:

ActiveSheet.Range(ActiveSheet.Cells(1, 1), ActiveSheet.Cells(3, 4)).Select

This also works avoiding the need to repeat several times the target worksheet: (see https://msdn.microsoft.com/EN-US/library/office/gg264723.aspx )

With ActiveSheet
    .Range(.Cells(1, 1), .Cells(3, 4)).Select
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM