Excel VBA range.find acting up

佐手、 提交于 2019-11-28 12:54:46

Try this

Public Function look_up_id(id, table) As Variant
    Dim ws As Worksheet
    Dim aCell As Range

    look_up_id = "Not Found"

    Set ws = ThisWorkbook.Sheets(table)

    With ws
        Set aCell = .Cells.Find(What:=id, _
                    LookIn:=xlFormulas, _
                    LookAt:=xlPart, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False, _
                    SearchFormat:=False)

        If Not aCell Is Nothing Then _
        look_up_id = aCell.Offset(, 1).Value
    End With
End Function

More on .Find HERE

Try to use this code instead (when Find doesn't find anything, it returns Nothing and then you tried to do sth like this Nothing.Activate, and this triggers an error):

Dim res As Range

Set res = Worksheets(table).Cells.Find(What:=id, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False)
If Not res Is Nothing Then
    look_up_id = res.Offset(0, 1).Value
End If
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!