Excel VBA 从 Selection.Address 获取行,单元格值


For i = 1 To 20
  '' select the cell in question
  Cells.Find(...).Select
  '' get the cell address
  CellAddr = Selection.Address(False, False, xlR1C1) 

Next

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

谢谢!

这是你要找的吗?

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
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

最新更新