简体   繁体   中英

i need interpretation of activecell.offset in vba

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.

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.

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.

These are the correct code

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM