vba 如何获取相邻单元格的地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4626775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:03:42  来源:igfitidea点击:

How to get address of adjacent cell?

excelvba

提问by xbonez

I have the following vba code to find occurrences of "0" in a following column:

我有以下 vba 代码可以在以下列中查找“0”的出现:

For Each c In Range("B:B")

        If c.Value = "0" Then
            MsgBox "0 found at " & (c.Address)
        End If
    Next c

How can I modify the code so that when it finds a "0" at say, B6, it displays C7? ie. it display the cell diagonally adjacent to the one where the "0" is found.

如何修改代码,以便当它在 B6 处找到“0”时,显示 C7?IE。它显示与找到“0”的单元格对角相邻的单元格。

采纳答案by Fionnuala

How about:

怎么样:

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

回答by Alex P

You can use Offset

您可以使用 Offset

MsgBox "0 found at " & c.Offset(1,1).Address

The Offsetproperty is of the form Offset(row, column). Examples:

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)