【问题标题】:excel vba getting the row,cell value from selection.addressexcel vba从selection.address中获取行,单元格值
【发布时间】:2013-09-30 20:07:20
【问题描述】:
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 对象,所以它可能会起作用我只是对实现感到模糊。

谢谢!

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    这就是你要找的吗?

    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
    

    【讨论】:

    • 我们当然应该可以参考:Selection.Address.Row 和 Selection.Address.Column,对吧?它对我不起作用,但我无法想象 VBA 大神会强迫我们使用 Split 来完成如此琐碎的任务??
    【解决方案2】:
    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 2013-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-29
      相关资源
      最近更新 更多