excel vba 从 selection.address 中获取行、单元格值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19102847/
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 16:47:02  来源:igfitidea点击:

excel vba getting the row,cell value from selection.address

excelvbaexcel-vba

提问by Brian

For i = 1 To 20

  '' select the cell in question
  Cells.Find(...).Select

  '' get the cell address

  CellAddr = Selection.Address(False, False, xlR1C1) 



Next

The above is my code for searching a spread sheet to find a specific string and then select it. I'd like to get the address of the cell using Selection.Addresswhich, in this case, returns something along the lines of R[100]C. Is there a way I can split that result in to row and column values so I can manipulate them in code? I'd like, for example to add 14 rows to the selected cells row value. I believe CellAddrwill be a Range object so it may work I'm just fuzzy on the implementation.

以上是我用于搜索电子表格以查找特定字符串然后选择它的代码。我想使用Selection.Address它来获取单元格的地址,在这种情况下,返回R[100]C. 有没有办法可以将结果拆分为行和列值,以便我可以在代码中操作它们?例如,我想将 14 行添加到选定的单元格行值。我相信CellAddr将是一个 Range 对象,所以它可能会起作用,我只是对实现感到模糊。

Thanks!

谢谢!

回答by Santosh

Is this what you are looking for ?

这是你想要的 ?

Sub getRowCol()

    Range("A1").Select ' example

    Dim col, row
    col = Split(Selection.Address, "$")(1)
    row = Split(Selection.Address, "$")(2)

    MsgBox "Column is : " & col
    MsgBox "Row is : " & row

End Sub

回答by Tim Williams

Dim f as Range

Set f=ActiveSheet.Cells.Find(...)

If Not f Is Nothing then
    msgbox "Row=" & f.Row & vbcrlf & "Column=" & f.Column
Else
    msgbox "value not found!"
End If