在 Excel 中使用 VBA 选择从 A1 到文件结尾的范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22487460/
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:59:14  来源:igfitidea点击:

Select range from A1 to end of file using VBA in Excel

excel-vbavbaexcel

提问by user12059

I want to select all values in Excel 2007 worksheet between A1 and end of file (effect of ctrl End). There are always 4 columns but the rows will range from 2 to possibly hundreds. There will possibly be lots of blank cells throughout the selection, including the last cell.

我想选择 Excel 2007 工作表中 A1 和文件结尾之间的所有值(ctrl End 的效果)。总是有 4 列,但行的范围从 2 到可能是数百。整个选择过程中可能会有很多空白单元格,包括最后一个单元格。

The following just goes to the last cell to be selected, not the entire range. How can I modify this to accomplish what I want?

以下仅转到要选择的最后一个单元格,而不是整个范围。我该如何修改它以完成我想要的?

ActiveSheet.Range("A1", SpecialCells(xlLastCell)).Select

Many thanks.

非常感谢。

回答by Rachel Hettinger

You almost have it. The SpecialCells method needs a qualifier:

你几乎拥有它。SpecialCells 方法需要一个限定符:

ActiveSheet.Range("A1", ActiveCell.SpecialCells(xlLastCell)).Select

回答by xQbert

Record a macro doing it then review the code:

录制一个宏,然后查看代码:

Something like this may work.

像这样的事情可能会奏效。

Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select

Running this macro selected the below in my example: enter image description here

运行此宏在我的示例中选择了以下内容: 在此处输入图片说明

回答by Gary's Student

If you always want the first four columns, then perhaps:

如果你总是想要前四列,那么也许:

Sub dural()
    Intersect(ActiveSheet.UsedRange, Range("A:D")).Select
End Sub