2011-01-07 26 views
5

我有以下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 ?即。它顯示與找到「0」的單元對角相鄰的單元。

回答

5

如何:

 MsgBox "0 found at " & Cells(c.Row + 1, c.Column + 1) 
+2

謝謝。它可以工作,但我必須讓它成爲Cells(c.Row + 1,c.Column + 1).Address來獲得地址,否則它會給我價值。 – xbonez 2011-01-07 15:20:57

18

您可以使用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) 
相關問題