Why does attempted match on a loop provide incorrect evaluation

upobDaPlaya used Ask the Experts™
Why does a an empty cell in a a named range = a non blank cell.  I am attempting to match cells in the data tab vs the named range and for some reason if the named range is blank it thinks it equals a cell from the data tab with a value of "orange" as an example.  I on purpose had my range in the named range exceed the cells where I have values in the event I later add to the named range..

Sub LoopData()

Dim intD As Integer
Dim intE As Integer
Dim lastrow As Integer
Dim lr As Range

Dim strProd As String
intD = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row

For d = 2 To intD
 strProd = Sheets("Data").Range("A" & d)
        For Each lr In Range("ItemFocus")
            If strproduct = lr.Value Then
                MsgBox (lr.Value)
            End If
         Next lr
 Next d

End Sub
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

you have 2 diferent variables

strProd  which has a value and

strproduct which is empty

In addition to the above statement, your data tab list "Orange" and name range lists "Oranges"!

Top Expert 2008
To avoid or at least minimize the issue of wrong variables used, write
Option Explicit

Open in new window

at the top of your modules, this will ensure that an error gets raised if you use an undefined variable (strproduct in this case).



Yikes...thanks for the solution and pointers

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial