我需要在 vba 中解释 activecell.offset

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

i need interpretation of activecell.offset in vba

excel-vbavbaexcel

提问by user3147731

I have some difficulty in understanding some VBA code. I have no problem with

我在理解某些 VBA 代码时遇到了一些困难。我没有问题

activecell.offset(1,1).select

However, I have problem with

但是,我有问题

activecell.offset(1,1).range("A1").select 

AND

ActiveCell.Offset(0, 3).Columns("A:A").EntireColumn.Select

Why is there a .range("A1") there? Why is there a .columns there? I read some other post saying that these things are not necessary. But I wrote some code using the same format, replacing .range("A1") with some other range and yielded a different results. Could you please explain these things to me? I mean the .range("A1") after offset. I inherited the code from someone else trying to understand.

为什么那里有 .range("A1") ?为什么那里有一个 .columns ?我读了一些其他的帖子,说这些东西是没有必要的。但是我使用相同的格式编写了一些代码,将 .range("A1") 替换为其他一些范围并产生了不同的结果。你能向我解释一下这些事情吗?我的意思是偏移后的 .range("A1") 。我从试图理解的其他人那里继承了代码。

回答by Stuart Taylor

The .range("A1") part you usually find added by Excel when you record a macro, even if you only select one cell. Essentially "A1" refers to the cell in the top-left-hand corner of the .activecell.offset position. So, if for example you changed this to:

.range("A1") 部分通常是在录制宏时由 Excel 添加的,即使您只选择了一个单元格。本质上,“A1”是指 .activecell.offset 位置左上角的单元格。因此,例如,如果您将其更改为:

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

The active cell would become the cell which is 0 rows, +1 column from the starting cell, then select an area 1 column and 3 rows in size, but here column A and rows 3 are RELATIVE references to the activecell.offset position, rather than refering to the worksheets column A, rows 1-3. Play around with "A1:A3" to see what I mean.

活动单元格将成为距起始单元格 0 行 +1 列的单元格,然后选择大小为 1 列 3 行的区域,但此处 A 列和第 3 行是对 activecell.offset 位置的相对引用,而不是而不是参考工作表 A 列第 1-3 行。玩弄“A1:A3”以了解我的意思。

The code might be simpler and just say

代码可能更简单,只是说

ActiveCell.Offset(0, 1).Select

which would be perfectly valid, but this code does not allow you to SELECT more than one cell. To do that, you need to use RANGE.

这将是完全有效的,但是此代码不允许您选择多个单元格。为此,您需要使用 RANGE。

回答by Biagiosoft

activecell.offset(1,1).range("A1").select should select one cell that is one cell down and one column on the right from the active cell.

activecell.offset(1,1).range("A1").select 应该选择一个单元格,该单元格在活动单元格的右侧一列。

ActiveCell.Offset(0, 3).Columns("A:A").EntireColumn.Select should select the third column on the right from the active cell.

ActiveCell.Offset(0, 3).Columns("A:A").EntireColumn.Select 应该选择活动单元格右侧的第三列。

回答by user4143192

These are the correct code

这些是正确的代码

Range("a1").Offset(1, 1).Select

Columns("A:A").EntireColumn.Offset(0, 3).Select