在 VBA 中获取表格范围(动态)

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

Get table range in VBA (dynamic)

excelvbafilterrange

提问by Mitchell Walker

I have a program in VBA that I am working on that filters values from a table. I'm trying to make this a generic program that works with all tables that you give it. In my program I have to set the range of the table that it is filtering: Set rng = dataSheet.Range("A1:F78"). I was wondering if there was a way to get the Range of a table in excel that has some sort of text value in it so I don't have to specify it in every macro. I guess this is a type of dynamic range.

我在 VBA 中有一个程序,我正在处理它从表中过滤值。我正在尝试使其成为一个通用程序,可以与您提供的所有表一起使用。在我的程序中,我必须设置它正在过滤的表的范围:Set rng = dataSheet.Range("A1:F78")。我想知道是否有办法在 excel 中获取包含某种文本值的表格的范围,因此我不必在每个宏中指定它。我想这是一种动态范围。

回答by Kazimierz Jawor

If there is one cell of your range which is always within your table, like A1would be always top-left corner of the table. And if there is continuous range of cells until the end of your table you could use .CurrentRegion propertyin this way:

如果您的范围内有一个单元格始终在您的表格内,则始终位于表格的A1左上角。如果在表格末尾之前有连续的单元格范围,您可以这样使用.CurrentRegion property

Set rng = dataSheet.Range("A1").CurrentRegion