在VBA excell宏的range语句中使用变量 [英] Using a variable in a range statement in VBA excell macro
问题描述
好吧,我确定这个问题确实很基本,但是我所有的搜索者都提出了复杂的答案,而且我已经将VBA弄乱了大约一天.我在excel文档中有两个工作表,我创建了一个按钮,我可以单击该按钮来调用我的宏,该宏只是将单元格从一个工作表移动到另一个工作表.但是我需要宏来确定我在哪一行.我正在使用这个:
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:
r = ActiveCell.Row
确定我的行,但是在这样的range语句中使用该变量的最简单方法是:
to determine my row, but what would be the easiest way to use that variable in a range statement like this:
Range("A2").Select
推荐答案
您可以将Range方法与&运算符,将变量连接到字符串中:
You could use the Range method with the & operator to join the variable into a string:
Range("A" & r)
或者,您可以使用带有参数(行,列) 的Cells方法:
Alternately you can use the Cells method which takes arguments (Row, Column):
Cells(r, "A")
这篇关于在VBA excell宏的range语句中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!