如何在 VBA 中选择特定单元格?

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

How do I select specific cells in VBA?

excelvbaexcel-vba

提问by gvar

Basically I need to select specific cells on an excel sheet in vba. For example the code should be able to select a5, a10, a15. I do not want the cells in between them, just the ones I listed. Is there a specific function that will do this? It seems like .Range could only take start cell and ending cell.

基本上我需要在vba的excel表上选择特定的单元格。例如代码应该可以选择a5、a10、a15。我不想要它们之间的单元格,只想要我列出的单元格。是否有特定的功能可以做到这一点?似乎 .Range 只能取开始单元格和结束单元格。

回答by Daniel

You'd use this: Range("A5,A10,A15").Select

你会用这个: Range("A5,A10,A15").Select

To add additional cells, just use more commas.

要添加其他单元格,只需使用更多逗号。

Alternately, you can utilize the Union methodto join multiple range objects into a single Range object.

或者,您可以使用Union 方法将多个范围对象合并为一个范围对象。

Note that it is generally not a good idea to select cells in VBA as nearly everything can be done without selection. This is a frequent mistake made by people new to VBA due to generated macros (Record Macro) recreating what you do rather than the result you want.

请注意,在 VBA 中选择单元格通常不是一个好主意,因为几乎所有事情都可以在没有选择的情况下完成。这是 VBA 新手经常犯的错误,因为生成的宏(记录宏)重新创建了您所做的事情而不是您想要的结果。