如何从excel vba中的Find函数获取单元格地址

时间:2016-12-07 03:05:11

标签: excel vba excel-vba find

我有一个问题,如何使用find函数获取单元格地址。这是代码

Dim Found As Range

Set Found = Worksheets("Sheet 1").Cells.Find(What:="test", LookAt:=xlWhole, MatchCase:=True)

    If Not Found Is Nothing Then
            ' do something
    End If

问题是当我尝试调试代码时,"发现"变量包含"字符串"而不是细胞地址。

不知道我的代码有什么问题。

3 个答案:

答案 0 :(得分:8)

即使它显示为字符串,您似乎也可以使用found.address。以下代码对我有用。

Sub findCellAddress()

    Dim ra As Range

    Set ra = Cells.Find(What:="fff", LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False, SearchFormat:=False)

    If ra Is Nothing Then
        MsgBox ("Not found")
        Else
        MsgBox (ra.Address)
    End If

End Sub

答案 1 :(得分:1)

我在Internet上的任何地方都找不到。 该代码将为您提供行和列。

    Dim ThisPos As Range
    With Range("A1:J100")
        Set ThisPos  = .Find(What:="List_Position", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
        If Not ThisPos  Is Nothing Then
            Cell_Add= Split(ThisPos.Address, "$")
            ThisRow = Cell_Add(1)
            ThisCol = Cell_Add(2)
        End If
    End With

答案 2 :(得分:0)

此代码将为您提供单元格地址的参考样式。

 Dim SValue As Range
   With Range("D1:D100")

    Set SValue = .Find(What:="Searched Value", LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
    
    If Not SValue Is Nothing Then
        Cell_Split_R = Split(SValue.Address(ReferenceStyle:=xlR1C1), "R")
        Cell_Split_C = Split(Cell_Split_R(1), "C")
        SCol = Cell_Split_C(0)
        SRow = Cell_Split_C(1)
        
    End If
End With