Excel VBA - 选择不包括标题的动态单元格范围

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

Excel VBA - Select a dynamic cell range excluding headers

excelvbaexcel-vba

提问by hugurlu

I'm trying to analyze huge amount of data in a spreadsheet. The records are being added,removed all the time. My headers are on the first rows, which I don't want to select. What I want to do is to select cells specifically starting from the cell B2, all the to the end of the records dynamically, both column wise and row wise. In other words select only the data records starting from the cell B2.

我正在尝试分析电子表格中的大量数据。记录一直在添加,删除。我的标题在第一行,我不想选择。我想要做的是专门从单元格 B2 开始选择单元格,所有到记录的末尾动态,列明智和行明智。换言之,仅选择从单元格 B2 开始的数据记录。

The code I've been trying to implement as follows so far:

到目前为止,我一直在尝试实现的代码如下:

Range(Cells(B2, Rows.Count).End(xlToLeft), Cells(2, Columns.Count).End(xlToLeft)).Select

which doesn't work. Any help appreciated!

这不起作用。任何帮助表示赞赏!

回答by Kazimierz Jawor

If your range is continuous you could try to do it this way:

如果您的范围是连续的,您可以尝试这样做:

Range("B2", range("B2").end(xlToRight).End(xlDown)).select

By the way, there could be plenty of other solutions with similar result. I've tried to do it as short as possible (based on information provided in question).

顺便说一句,可能还有很多其他解决方案具有类似的结果。我已尝试尽可能短(根据相关信息提供)。

回答by dra_red

There are quite a few ways to do this. I find I use different approaches based on the structure of the data. One way is:

有很多方法可以做到这一点。我发现我根据数据的结构使用了不同的方法。一种方法是:

activesheet.range("B2:" &  Activesheet.cells(2,2).specialcells(xlcelltypelastcell).address).select

You can either use the range address as I did above or you can supply the end and start cells as in: With ActiveSheet .Range(.Cells(2, 2), .Cells(.UsedRange.Rows.Count, .UsedRange.Columns.Count)).Select End With

您可以像我上面那样使用范围地址,也可以提供结束和开始单元格,如下所示:使用 ActiveSheet .Range(.Cells(2, 2), .Cells(.UsedRange.Rows.Count, .UsedRange.Columns .Count)).选择结尾

Also, for reference, there are options such as intersect and union that can be useful: With ActiveSheet Application.Intersect(ActiveSheet.UsedRange, Rows("2:1048576")).Select End With

此外,作为参考,还有一些有用的选项,例如 intersect 和 union:With ActiveSheet Application.Intersect(ActiveSheet.UsedRange, Rows("2:1048576")).Select End With

It goes on and on. I think the first example will most likely get you out of trouble...

它会一直持续下去。我认为第一个例子很可能会让你摆脱困境......