Search value in VBA

mehidy1437

Active Member
Joined
Nov 15, 2019
Messages
348
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
  2. Mobile
  3. Web
Hello Guys

I'm facing an issue with searching value in excel in VBA.
When I search a value, if the value only exist in a1 then below code give me the resul a1.
But if same value exist in a1 & in any other cells like in a2 or b2, it gives me the result b2 or a2 i/o a1.

Why is this happening?
VBA Code:
Sub SearchValues()
    Dim searchValue As Variant
    Dim searchRange As Range
    Dim foundCell As Range
    
    searchValue = "Style"
    Set searchRange = Range("A:Z")
    
    Set foundCell = searchRange.Find(What:=searchValue, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False, SearchDirection:=xlNext, SearchOrder:=xlByRows, SearchFormat:=False)
    
    If Not foundCell Is Nothing Then
        MsgBox "Value found in cell " & foundCell.Address
    Else
        MsgBox "Value not found in the search range."
    End If
End Sub
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
The Find method actually begin the search after the first cell (i.e A1), in this case cell B1.
Try this:
VBA Code:
Sub SearchValues()
    Dim searchValue As Variant
    Dim searchRange As Range
    Dim foundCell As Range
   
    searchValue = "Style"
    Set searchRange = Range("A:Z")
    If UCase(Range("A1")) Like "*" & UCase(searchValue) & "*" Then
         MsgBox "Value found in cell " & Range("A1").Address
    Else
        Set foundCell = searchRange.Find(What:=searchValue, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False, SearchDirection:=xlNext, SearchOrder:=xlByRows, SearchFormat:=False)
       
        If Not foundCell Is Nothing Then
            MsgBox "Value found in cell " & foundCell.Address
        Else
            MsgBox "Value not found in the search range."
        End If
    End If
End Sub
 
Upvote 0
Solution
Thanks, it's working fine.
If find start searching after a1, then why the code in #1, give me the result a1, when the value "Style" is not present in another cell except a1?
 
Upvote 0
If find start searching after a1, then why the code in #1, give me the result a1, when the value "Style" is not present in another cell except a1?
Here's why:
Search starts from B1 > then to the right, namely c1, d1, ....z1 (the last column in Range("A:Z")) > then to the next row > this process continues until the last row then returns to A1.
So actually A1 is the last cell to search.
 
Upvote 0
Here's why:
Search starts from B1 > then to the right, namely c1, d1, ....z1 (the last column in Range("A:Z")) > then to the next row > this process continues until the last row then returns to A1.
So actually A1 is the last cell to search.
Many thanks for the details information.
 
Upvote 0
You're welcome.
There's another way to do it, which is using "After" argument, but you need to know where the last cell is.
For example, say the search range is Range("A1:Z100"):
Rich (BB code):
Sub SearchValues1()
    Dim searchValue As Variant
    Dim searchRange As Range
    Dim foundCell As Range
    
    searchValue = "Style"
    Set searchRange = Range("A1:Z100")

        Set foundCell = searchRange.Find(What:=searchValue, After:=Range("Z100"), LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False, SearchDirection:=xlNext, SearchOrder:=xlByRows, SearchFormat:=False)
        
        If Not foundCell Is Nothing Then
            MsgBox "Value found in cell " & foundCell.Address
        Else
            MsgBox "Value not found in the search range."
        End If

End Sub

the search will start at A1.
 
Upvote 0

Forum statistics

Threads
1,223,931
Messages
6,175,465
Members
452,646
Latest member
tudou

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top