Macro to find a value in a worksheet

[Closed]
Report
Posts
17
Registration date
Thursday March 3, 2011
Status
Member
Last seen
July 20, 2011
-
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
-
Hello,

I am new with excel macro and VBA codes.Can someone out there help me with the macro and VBA code to create an input boxes to seacrh and find a value in a list of a worksheet.

worksheet name:(Sheet2)B2JT
Range to seach :A9:AA39

Thanks.

1 reply

Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
768
If you are new to macro, a very good starting point would be using macro recorder. It can give a general template for what you need.

Here is a macro that can do what you wanted

Sub doExecuteSearch()

   Dim sSearchFor          As String
   Dim rngCell             As Range
   
   Do
      
      sSearchFor = InputBox("Please enter value to be searched", "Search")
      If sSearchFor <> vbNullString _
      Then
         With Sheets("B2JT")
            Set rngCell = .Range("a2:a9").Find(sSearchFor, .Cells(8, 1), , xlWhole, xlByRows, xlNext)
            If Not rngCell Is Nothing _
            Then
               MsgBox "found at row " & rngCell.Row & " and column " & rngCell.Column
            Else
               MsgBox "Not found"
            End If
         End With
      End If
   
   Loop While sSearchFor <> vbNullString
   
End Sub