[vba] exact value search

Solved/Closed
cel92 Posted messages 28 Status Member -  
 Mimi -
Hello,

As a beginner in VBA,
I am looking for the value "NumUnique" among the cells "B39 to B200" using the code:
"
Set CelluleTrouvee = Range("B39:B200").Find(NumUnique, LookIn:=xlValues)
"

PROBLEM: my macro does not return the cells with a value EQUAL to the NumUnique value sought, but the cells CONTAINING NumUnique.
For example, if I search for NumUnique = 2,
the result is CelluleTrouvee.Value = 52

Do you know another function that returns a cell containing the exact searched value?
Otherwise, do you have a simple example to work around the problem?

Thank you in advance!
Céline
Configuration: Windows XP Internet Explorer 6.0

6 answers

  1. tompols Posted messages 1273 Registration date   Status Contributor Last intervention   460
     
    Hello,

    The Find method has an optional argument of type variant "Lookat", with possible values being xlWhole or xlPart...
    Referring back to your example, it goes like this:

    Public Sub test()

    Dim number As Integer
    Dim foundCell As Range
    Dim row As Integer
    Dim col As Integer

    number = 8

    Set foundCell = Range("A1:A5").Find(number, lookat:=xlWhole)

    If foundCell Is Nothing Then
    MsgBox ("not found")
    Else
    row = foundCell.Row
    col = foundCell.Column
    MsgBox ("found: row = " & row & " , column = " & col)
    End If

    End Sub
    54
    1. hich24 Posted messages 1686 Status Member 753
       
      thank you
      0
    2. Florian
       
      You're welcome!
      0
    3. Cdric
       
      You're welcome!
      0
    4. Mimi
       
      Thank you, very useful line of code!
      0