Search code examples
excelvbacell

How to search for a value in a cell two rows over from first cell that meets criteria?


I created a for each loop to search for cells that match a value in all my worksheets for the active workbook it works fine but I need to check the value on the second row to the right of the cell that matches. So if it matches in a2 it will need to check the contents of c2.

I don't know how to reference the adjacent cells to the match. I will like to be able to do something like. If cel.value like "boston*" and the cell in the same row two columns over like "mass*" then do whatever

 For Each ws In ActiveWorkbook.Worksheets

    ws.Activate


    For Each cel In rngToSearch.Cells

        With cel


            If cel.Value Like "boston*" Or cel.Value Like "manfield*" Or 
cel.Value Like "barnes*" Or cel.Value Like "langley*" Then


                Set vsoShape = 
Application.ActiveWindow.Page.Drop(Application.DefaultRectangleDataObject, 
aoffset, boffset)

                vsoShape.Text = cel.Value


            Else

            End If



        End With

    Next cel
    Next ws

I need to be able to check the value of the cell two rows over from the match if the match is in a2 I will need to also be able to check c2. Thanks


Solution

  • You may need this:

    With cel
            If (.Value Like "boston*" Or _
                .Value Like "manfield*" Or _
                .Value Like "barnes*" Or _
                .Value Like "langley*") _
             And .Offset(0, 2).Value Like "mass*" Then