简体   繁体   中英

How to activate a range in excel VBA using ActiveCell.Offset

I am trying to activate the range 4 across and 'n' down from the current active cell. I am new to VBA and programming as a whole.

I have tried putting each "ActiveCell.Offset(0, 0)" in its own parentheses and without. It also works with the first as "ActiveCell.Offset(0, 1)" but I want the upper left cell in the activated range to be the original ActiveCell

     Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(n, 4)).Activate

When I put ActiveCell.Offset(0, 0) in their own parentheses I get an error message "method 'range' of object '_global' failed". Ran without the parentheses it just does nothing at all without an error.

From the documentation

Range.Activate method

Activates a single cell, which must be inside the current selection. To select a range of cells, use the Select method.

So, your code could be

Range(ActiveCell, ActiveCell.Offset(n, 4)).Select

That said, consider not using Activate/Select at all. See here for how .

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