VBA - Find string in range not working properly

wibni

New Member
Joined
Jun 15, 2011
Messages
33
Hi,

I'm using Excel 2003 and am working on the table below.
My VBA code runs through column H and if it finds a matching number in D1:E4 it returns the value in column C for that row.

So for example '005' in H6 is found in D2, therfore copy C2 ('NMM') into I6
This works pretty well, however for some reason '008' in H7 does not return GGM. Instead it returns MWA.

I thought it always returns the first hit but that doesn't seem to be the case.
How can I exclude rows which have 'Office' in column B from my search range.

Can I somehow define exceptions in 'Find'?
Code:
Set found = rngSites.Find(cell.Value, , xlValues, xlWhole, xlByRows, xlNext, False)
This is my whole code:
Code:
Sub Site_Lookup()
    
    Dim rngSites As Range, rngLookup As Range, cell As Range, found As Range
    Dim LR As Long, i As Long
            
    LR = Range("H" & Rows.Count).End(xlUp).Row
    Const FR As Long = 6 '<-- First Row of actual data
    
        
    Set rngSites = Range("D" & 1 & ":E" & 4)
    Set rngLookup = Range("H" & FR & ":H" & LR)

    For Each cell In rngLookup
        If cell.Value <> "" Then
            If IsNumeric(cell) Then
                Set found = rngSites.Find(cell.Value, , xlValues, xlWhole, xlByRows, xlNext, False)
                If Not found Is Nothing Then
                    ' Site found
                    cell.Offset(, 1).Value = Range("C" & found.Row).Value
                Else
                    ' No Match
                    cell.Offset(, 1).Value = "N\A"
                End If
            Else
                ' Text
                cell.Offset(, 1).Value = cell.Value
            End If
        End If
    Next cell
    
End Sub

Excel Workbook
ABCDEFGHI
1JanuarySiteGGM008024
2JanuarySiteNMM005011
3JanuaryOfficeMWA008024
4JanuaryOfficeKNC008024
5RigSiteRigSite
6005NMM
7008MWA
8011NMM
9021N\A
10024GGM
Sheet1
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Forum statistics

Threads
1,224,612
Messages
6,179,890
Members
452,948
Latest member
Dupuhini

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