在 VBA Excel 宏的范​​围语句中使用变量

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

Using a variable in a range statement in VBA excell macro

excelvbaexcel-vba

提问by Jeff

Ok, I'm sure this question is really basic but all my searchers turn up complicated answers and I've been messing with VBA for about a day. I have two worksheets in an excel doc, I've created a button that I can click that invokes my macro that is just moving cells from one work sheet to another. But I need my macro to determine what row I am on. I'm using this:

好的,我确定这个问题非常基础,但我所有的搜索者都给出了复杂的答案,而且我已经使用 VBA 大约一天了。我在 excel 文档中有两个工作表,我创建了一个按钮,我可以单击该按钮调用我的宏,该宏只是将单元格从一个工作表移动到另一个工作表。但是我需要我的宏来确定我在哪一行。我正在使用这个:

r = ActiveCell.Row

to determine my row, but what would be the easiest way to use that variable in a range statement like this:

来确定我的行,但是在这样的范围语句中使用该变量的最简单方法是什么:

Range("A2").Select

回答by tigeravatar

You could use the Range method with the & operator to join the variable into a string:

您可以使用带有 & 运算符的 Range 方法将变量连接到字符串中:

Range("A" & r)

Alternately you can use the Cells method which takes arguments (Row, Column):

或者,您可以使用带有参数(Row, Column)的 Cells 方法:

Cells(r, "A")