首页 > 解决方案 > 查找“Cells.Find”函数的单元格引用

问题描述

我正在使用下面的代码来查找用户搜索的值(“strFindWhat”),该值被输入到单元格中,然后按下按钮来触发这个子。系统包含一长串数据,用户将搜索产品编号,以便他们可以快速看到相应的批号。

Cells.Find(What:=strFindWhat, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False).Select

我想要做的是找到此代码找到的单元格的引用。这样我就可以更改找到的单元格的颜色或行,以突出显示他们需要的数据,以便他们更清楚地看到它。我通过使整个函数等于一个变量来尝试明显的:

foundCell = Cells.Find(What:=strFindWhat, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False).Select

“foundCell”的值总是空白,我希望有人知道找到对找到的单元格的引用的方法?

标签: vbaexcel

解决方案


为了找到地址,你可以这样做

Dim foundCell As Range
    Set foundCell = Cells.Find(What:=strFindWhat, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
             :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
                        False)

    If Not (foundCell Is Nothing) Then
        Debug.Print foundCell.Address
    End If

推荐阅读