Excel VBA - 选择到列末尾减去两行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20206389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:17:17  来源:igfitidea点击:

Excel VBA - select to end of column minus two rows

excelvbaexcel-vba

提问by Petrov

I'm in a situation where I want to start from a cell, E9 say, and I want to select down to the end of the column EXCEPT I don't want to include the last 2 cells in the column.

我处于一种情况,我想从一个单元格开始,比如 E9,我想选择到列的末尾,除了我不想在列中包含最后 2 个单元格。

So for example

所以例如

A
B
C
D
E

I want to select the range A:C in the column but NOT D and E.

我想在列中选择范围 A:C 但不是 D 和 E。

Have tried

试过

ActiveSheet.Range("E9", ActiveSheet.Range("E9").End(xlUp)).Select

but don't know how to lop off the last two cells...

但不知道如何砍掉最后两个单元格...

回答by tigeravatar

Range("E9", Cells(Rows.Count, "E").End(xlUp).Offset(-2)).Select

Though as a rule of thumb, you should avoid Select statements.

尽管根据经验,您应该避免使用 Select 语句。

回答by Tim Williams

Dim r as range

set r = activesheet.range(activesheet.range("A1"), _
                        activesheet.cells(rows.count,1).end(xlup))

if r.rows.count>2 then set r=r.resize(r.rows.count-2,1)