VBA to find a specified cell value in specified range and select it

后端 未结 1 1449
你的背包
你的背包 2021-01-27 14:25

I am having trouble creating a macro that will find a specified value in my active sheet within a range in my \"Information\" sheet. If the cell value us not found in the range

相关标签:
1条回答
  • 2021-01-27 15:10

    You want to avoid .Select/.Activate

    Sub testRot2()
    Dim str As String
    Dim srchRng As Range
    With Worksheets(ActiveSheet.Name)
        str = .Cells(2, 5).Value
        Set srchRng = .Range("J8:J17").Find(what:=str)
        If srchRng Is Nothing Then
            MsgBox "Not found"
        End If
    End With
    End Sub
    

    Also, note that I changed i to str. This is a personal choice, as I typically see i as a Long/Integer for looping. You can keep it i though, just thought to mention it. Also, note the colon required in Find(what:=str).

    0 讨论(0)
提交回复
热议问题