Excel VBA 将活动单元格设置为范围

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

Excel VBA set active cell as range

excelvbaexcel-vba

提问by Tim Wilkinson

Using VBA I am selecting the first empty cell in a column using the following

使用 VBA 我使用以下选择列中的第一个空单元格

Range("A3").End(xlDown).Offset(1, 0).Select

How can I set this currently active cell as a range for calling later in the macro. I tried,

如何将此当前活动的单元格设置为稍后在宏中调用的范围。我试过,

Dim Target as Range
Range("A3").End(xlDown).Offset(1, 0).Select
Target = Cells(Application.ActiveCell)

My apologies if this is a basic question.

如果这是一个基本问题,我很抱歉。

回答by barryleajo

If you mean the first empty cell at the end of the column then this is the slight variation required. I would counsel against using the variable name Targetbecause this is also a VBA keyword.

如果您的意思是列末尾的第一个空单元格,那么这就是所需的细微变化。我建议不要使用变量名,Target因为这也是 VBA 关键字。

Dim rng As Range
Set rng = Cells(Rows.Count, "A").End(xlUp).offset(1,0)
MsgBox rng.Address

回答by Gareth

You don't need to select the cell first and as you're assigning an object, you need to use Setlike so:

您不需要先选择单元格,并且在分配对象时,您需要Set像这样使用:

Dim Target As Range
Set Target = Range("A3").End(xlDown).Offset(1,0)

回答by Gene Skuratovsky

Here it is:

这里是:

Dim Target as Range
Set Target = Range("A3").End(xlDown).Offset(1, 0)