Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50158

[SOLVED] Search Multiple columns for string, display matching results on different sheet.

$
0
0
Hey Guys,

Ripped this code from another site, it runs fine with no errors but never finds any results.
Tried tweaking it a couple of different ways to no avail. Anyone have any ideas how to get this to work? I just want to search columns A to E and display the matching Rows (columns A to F) on the sheet 'Search Results'

Code:

Sub Search()
    'Standard Sheet Module code, like: Sheet1.
    Dim iFound
    Dim Message$, Title$, Default$, myCode$
    Dim cell1, cell2, rng As Range
    Set cell1 = Cells(2, 1)
    Set cell2 = cell1.End(xlDown).End(xlToRight)
    Set rng = Range(cell1, cell2)
    Message = "Enter Search Data:" ' Set prompt.
    Title = "Search" ' Set title.
    Default = "" ' Set default.
    ' Display message, title, and default value.
    myCode = InputBox(Message, Title, Default)

    iFound = False
    Application.ScreenUpdating = False

    'Check data sheet for data wanted!
    Worksheets("Box Index").Select
    For Each r In Worksheets("Box Index").UsedRange.Columns
    n = r.Column

    If Worksheets("Box Index").Cells(n, 1) = myCode Then
    iFound = True
    'Copy the found data from the starting row = myStart to
    'the ending row = myFinish for the Found Date [myDate]
    'to Sheet2 in column "C" change as needed!
    'Start data import just below any entery in this column!
    Worksheets("Box Index").Range(Cells(1, n), Cells(5, n)).Copy _
    Destination:=Worksheets("Search Results").Range("C65536").End(xlUp).Offset(1, 0)
    Else

    End If
    Next r

    If iFound = False Then MsgBox "No Results Found"
    Application.CutCopyMode = True
    Application.ScreenUpdating = True
    End Sub


Viewing all articles
Browse latest Browse all 50158

Trending Articles