i need interpretation of activecell.offset in vba

2019-07-15 11:39发布

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

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.

3条回答
迷人小祖宗
2楼-- · 2019-07-15 11:45

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:

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.

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.

查看更多
Explosion°爆炸
3楼-- · 2019-07-15 11:52

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(0, 3).Columns("A:A").EntireColumn.Select should select the third column on the right from the active cell.

查看更多
手持菜刀,她持情操
4楼-- · 2019-07-15 11:58

These are the correct code

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

Columns("A:A").EntireColumn.Offset(0, 3).Select
查看更多
登录 后发表回答