Really new to VBA here... I've looked around and tried to piece together some code to fulfil my need. Think it's almost there, but I'm getting errors that are likely easy to overcome and yet I don't know how.
The code looks at the current sheet (STOCK), and takes a 'target' text value from cell A2. It then searches a named range in another sheet 'Other'. If it determines one of the cells ('cand') in Other to be equal to the target value, then a value of "True" will be applied to column G in the STOCK sheet, on the same row of the original target.
Hopefully this makes sense. I've copied in the code which will maybe shed more light on things.
Dim target As String Dim cand As String Dim currentrow As Integer Sub search_named_range() ' This range is hard coded; we can try A:A if hard code version works ' For Each target In Worksheets("STOCK").Range("A2:A1000") ' retrieve the row of the current range, for use when setting target values ' currentrow = Range(target).Row ' FOR loop to search range of part numbers in Mojave ' For Each cand In Worksheets("Other").Range("N9:N150") If StrConv(cand.Value, 2) = StrConv(target, 2) Then Worksheets("STOCK").Range("G" + currentrow) = "True" GoTo FORend End If Next cand ' If part is not found, do nothing and return to find next target ' FORend: Next target End Sub
Currently I'm getting the error 'For Each control variable must be Variant or Object', but can't find anywhere that explains why this is. I'm sure it's pretty obvious, but a steer would be really appreciated.
Thanks.