Consulting

Results 1 to 3 of 3

Thread: VBA Edit to Search Range

  1. #1
    VBAX Regular
    Joined
    Apr 2011
    Posts
    25
    Location

    VBA Edit to Search Range

    Hello Experts,
    I'm still relatively new to VBA, and need some assistance with what I hope is a minor tweak. I'd like for my code below to search a range instead of a specific cell. I'm fairly certain that to do so the output (returned values) will also have to be tweaked - is this possible?

    Sub Return_Results_Sheet()
    searchValue = Range("A6")
    searchCol = 40
    returnValueCol = 37
    outputValueCol = 8
    outputValueRowStart = 6
    lastRow = Cells(Rows.Count, searchCol).End(xlUp).Row
    Range(Cells(outputValueRowStart, outputValueCol), Cells(Rows.Count, outputValueCol)).Clear
    For i = 1 To lastRow
        checkValue = Cells(i, searchCol).Value
        If checkValue = searchValue Then
            returnvalue = Cells(i, returnValueCol)
            nextOutputRow = Cells(Rows.Count, outputValueCol).End(xlUp).Row + 1
            If nextOutputRow < outputValueRowStart Then
                nextOutputRow = outputValueRowStart
            End If
            Cells(nextOutputRow, outputValueCol).Value = returnvalue
        End If
    Next i
    End Sub

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    It currently does search a range, but I guess you want to search that range for a number of different things. I'm going to assume things in cells A6:A8:
    Sub Return_Results_Sheet()
    searchValues = Range("A6:A8")  'your range of things being sought.
    searchCol = 40
    returnValueCol = 37
    outputValueCol = 8
    outputValueRowStart = 6
    lastRow = Cells(Rows.Count, searchCol).End(xlUp).Row
    Range(Cells(outputValueRowStart, outputValueCol), Cells(Rows.Count, outputValueCol)).Clear
    For i = 1 To lastRow
      For Each searchValue In searchValues
        checkValue = Cells(i, searchCol).Value
        If checkValue = searchValue Then
          returnvalue = Cells(i, returnValueCol)
          nextOutputRow = Cells(Rows.Count, outputValueCol).End(xlUp).Row + 1
          If nextOutputRow < outputValueRowStart Then
            nextOutputRow = outputValueRowStart
          End If
          Cells(nextOutputRow, outputValueCol).Value = returnvalue
          'Cells(nextOutputRow, outputValueCol).Offset(, 1).Value = searchValue
        End If
      Next searchValue
    Next i
    End Sub
    There's a commented-out line which if you enable will give the item being sought in the column to the right of the results column.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Regular
    Joined
    Apr 2011
    Posts
    25
    Location
    Exactly what I was needing done...thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •