我有以下vba代码,可在以下列中找到“0”的出现位置:
For Each c In Range("B:B")
If c.Value = "0" Then
MsgBox "0 found at " & (c.Address)
End If
Next c
如何修改代码,以便在B6找到“0”时显示C7?即。它显示与找到“0”的那个对角相邻的单元格。
答案 0 :(得分:24)
您可以使用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)
答案 1 :(得分:8)
怎么样:
MsgBox "0 found at " & Cells(c.Row + 1, c.Column + 1)