如何获取相邻单元格的地址?
我有以下vba代码来查找在下面的列中出现“0”:
For Each c In Range("B:B") If c.Value = "0" Then MsgBox "0 found at " & (c.Address) End If Next c
我怎样才能修改代码,当它发现一个“0”,B6,它显示C7? 即。 它显示与find“0”的单元对angular相邻的单元格。
怎么样:
MsgBox "0 found at " & Cells(c.Row + 1, c.Column + 1)
你可以使用Offset
MsgBox "0 found at " & c.Offset(1,1).Address
Offset
属性的格式为Offset(row, column)
。 例子:
Range("B6").Offset(0,0) //refers to cell B6 Range("B6").Offset(1,0) //move one row down (B7) Range("B6").Offset(0,1) //move one column to the right (C6) Range("B6").Offset(-1,0) //move one row up (B5) Range("B6").Offset(0,-1) //move one column to the left (A6)