You cannot use the String variable in the For Everyone line. You use tartget and cand as control variables in For Each loops, but you defined them as strings. They must be an object, and specifically an object that contains a collection of objects that you iterate. You are repeating a range, which is a set of ranges, so your control variables must be range objects.
Sub search_named_range() Dim rCell As Range Dim rCand As Range For Each rCell In Worksheets("STOCK").Range("A2:A1000").Cells For Each rCand In Worksheets("Other").Range("N9:N150").Cells If StrComp(rCand.Value, rCell.Value, vbTextCompare) = 0 Then rCell.Offset(0, 6).Value = "True" Exit For 'exits the rCand For, but no the rCell one End If Next rCand Next rCell End Sub
Other changes that did not fix the error:
I'm not sure why you declared your variables outside of sub, but I put them inside.
You don't need to define .Cells at the end of the For Each line, but I like it. You can .Rows over .Rows or .Columns or .Areas using a range (although .Cells is the default value).
There is nothing wrong with StrConvert, but you can also use LCase () or, like me, StrComp.
Since I already have a cell reference in the current row (rCell), I use this and Offset to populate the column I want.
source share