vba ActiveCell.Offset 混淆

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

ActiveCell.Offset Confusion

excelvbaexcel-vba

提问by mburke05

I was reading some VBA in a previous module somebody had written and came across something that confused me:

我正在阅读某人之前编写的模块中的一些 VBA 并遇到了一些让我感到困惑的事情:

Sheets("Setup").Select
Range("Start").Select
ActiveCell.Offset(1, 0).Range("A1").Select

I was wondering how the ActiveCell.Offset(column,row).Range().Select line worked. In this case the "Start" range is a cell, A18, and the offset offsets it by one row, that much I get. But I'm not following how or what the Range("A1") is being inserted to do here.

我想知道 ActiveCell.Offset(column,row).Range().Select 行是如何工作的。在这种情况下,“开始”范围是一个单元格,A18,偏移量将它偏移一行,我得到了这么多。但我没有关注 Range("A1") 在这里插入的方式或内容。

Wouldn't

不会

Sheets("Setup").Select
Range("Start").Select
ActiveCell.Offset(1, 0).Select

work just the same and be less confusing? Is there any reason the Range("A1") clause is inserted?

工作相同并且不那么令人困惑?插入 Range("A1") 子句是否有任何原因?

Thanks so much, and sorry for the beginner question.

非常感谢,很抱歉初学者的问题。

回答by chancea

Short answer

简答

Yes in this particular caseboth do the same. Removing Range("A1")is fine.

是的,在这种特殊情况下,两者都做同样的事情。去掉Range("A1")就好了。

Long Answer

长答案

This is due to the fact you are using ActiveCellin this line:

这是由于您ActiveCell在此行中使用的事实:

ActiveCell.Offset(1, 0).Range("A1").Select

The ActiveCellis the first cell within the rangeyou have selected.

ActiveCell是第一个单元格范围内您选择。

Consider the following macro:

考虑以下宏:

Sub Macro1()
    Debug.Print ActiveCell.Address
End Sub

Whatever range you select this will print the address of the white cellwithin the selection.

无论您选择什么范围,这都会打印所选内容中白色单元格的地址。

i.e.

IE

enter image description here

在此处输入图片说明

The ActiveCellis $A$4

ActiveCell$A$4

Example

例子

Calling Offset(1,0)on a single cellwill only offset that cell. So if we look at your original code:

调用Offset(1,0)单个细胞只能抵消该小区。因此,如果我们查看您的原始代码:

Sub Macro2()
    Sheets("Setup").Select
    Range("Start").Select
    ActiveCell.Offset(1, 0).Range("A1").Select
End Sub

Let us assume that my previous selection shown (A2:B4) is the named range of "Start" and we can walk through exactly what is happening:

让我们假设我之前显示的选择 ( A2:B4) 是“开始”的命名范围,我们可以准确地了解正在发生的事情:

  1. In this example Range("Start").Selectwould select range A2:B4. Thus making ActiveCellequal to A2.

  2. Next we call Offset(1,0)on ActiveCellwhich is equivalent to Range("A2").Offset(1,0)putting us at range A3(1 row below A2)

  3. Now we call .Range("A1")which is going to grab the first cellwithin the range. Since the current range is onlyA3, .Range("A1")gives us A3.

  4. Then of course .Select()is still only selecting A3

  1. 在这个例子Range("Start").Select中将选择 range A2:B4。从而使ActiveCell等于A2

  2. 接下来,我们呼吁Offset(1,0)有关ActiveCell这相当于Range("A2").Offset(1,0)把我们的范围A3(1行下面A2)

  3. 现在我们调用.Range("A1")which 将抓取范围内的第一个单元格。由于当前范围仅为A3.Range("A1")给了我们A3

  4. 那么当然.Select()还是只选A3

When is .Range("A1")actually useful?

什么时候.Range("A1")真正有用?

Consider the following example without any Range("A1")call:

考虑以下没有任何Range("A1")调用的示例:

Sub Macro3()
    Sheets("Setup").Select
    Range("Start").Select
    Selection.Offset(1, 0).Select
End Sub

Since we have changed ActiveCellto Selectionthe Offset(1,0)will select the same dimension range as "Start" just offset-ed by 1 row.

既然我们已经改变ActiveCellSelectionOffset(1,0)会选择相同的尺寸范围为“开始”只是1行偏移-ED。

i.e.:

IE:

If this is the range of "Start":

如果这是“开始”的范围:

enter image description here

在此处输入图片说明

We run the example macro:

我们运行示例宏:

enter image description here

在此处输入图片说明

We have a new selection of the same dimension.

我们有一个相同维度的新选择。

However if we change the example macro to include the Range("A1"):

但是,如果我们更改示例宏以包含Range("A1")

Sub Macro4()
    Sheets("Setup").Select
    Range("Start").Select
    Selection.Offset(1, 0).Range("A1").Select
End Sub

enter image description here

在此处输入图片说明

Only the firstcell in the selection is now selected.

现在只选择了选择中的第一个单元格。