Results 1 to 4 of 4

How to create a search button

Threaded View

  1. #1
    Registered User
    Join Date
    02-15-2010
    Location
    Sydney, NS
    MS-Off Ver
    Excel 2007, MS office
    Posts
    2

    How to create a search button

    I am looking to have a search button actually search through all my sheets, i want it to search through column A, numbers & text. I have inserted a search button and a text box, but i can't get either to work.

    This is my code i have picked up from other sites, can someone please help!
    Thanks in advance
    Sub myFind()
    'Standard module code, like: Module1.
    'Find my data and list found rows in report!
    Dim rngData As Object
    Dim strDataShtNm$, strReportShtNm$, strMySearch$, strMyCell$
    Dim lngLstDatCol&, lngLstDatRow&, lngReportLstRow&, lngMyFoundCnt&
    
    On Error GoTo myEnd
    '*******************************************************************************
    strDataShtNm = "Sheet2" 'This is the name of the sheet that has the data!
    strReportShtNm = "Sheet1" 'This is the name of the report to sheet!
    '*******************************************************************************
    Sheets(strReportShtNm).Select
    Application.ScreenUpdating = False
    
    'Define data sheet's data range!
    Sheets(strDataShtNm).Select
    
    With ActiveSheet.UsedRange
    lngLstDatRow = .Rows.Count + .Row - 1
    lngLstDatCol = .Columns.Count + .Column - 1
    End With
    
    Set rngData = ActiveSheet.Range(Cells(1, 1), Cells(lngLstDatRow, lngLstDatCol))
    
    'Get the string to search for!
    strMySearch = InputBox("Enter what to search for, below:" & vbLf & vbLf & _
    "Note: The search is case sensitive!", _
    Space(3) & "Find All", _
    "")
    
    'Do the search!
    For Each Cell In rngData
    strMyCell = Cell.Value
    
    'If found then list entire row!
    If strMyCell = strMySearch Then
    lngMyFoundCnt = lngMyFoundCnt + 1
    ActiveSheet.Rows(Cell.Row & ":" & Cell.Row).Copy
    
    With Sheets(strReportShtNm)
    'Paste found data's row!
    lngReportLstRow = .UsedRange.Rows.Count + .UsedRange.Row
    ActiveSheet.Paste Destination:=.Range("A" & lngReportLstRow).EntireRow
    End With
    End If
    Next Cell
    
    myEnd:
    'Do clean-up!
    Application.ScreenUpdating = True
    Application.CutCopyMode = False
    Sheets(strReportShtNm).Select
    
    'If not found then notify!
    If lngMyFoundCnt = 0 Then
    MsgBox """" & strMySearch & """" & Space(3) & "Was not found!", _
    vbCritical + vbOKOnly, _
    Space(3) & "Not Found!"
    End If
    End Sub
    Private Sub CommandButton1_Click()
    
    End Sub
    
    Sub Button3_Click()
    'Standard Sheet Module code, like: Sheet1.
    Dim iFound
    Dim Message$, Title$, Default$, myCode$
    
    Message = "Enter data to search for:" ' Set prompt.
    Title = "Get Data!" ' 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("Sheet1").Select
    For Each r In Worksheets("Sheet1").UsedRange.Columns
    n = r.Column
    
    If Worksheets("Sheet1").Cells(1, n) = 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("Sheet1").Range(Cells(2, n), Cells(4, n)).Copy _
    Destination:=Worksheets("Sheet2").Range("C65536").End(xlUp).Offset(1, 0)
    Else
    
    End If
    Next r
    
    If iFound = False Then MsgBox "Date not Found!"
    Worksheets("Sheet2").Select
    Application.CutCopyMode = True
    Application.ScreenUpdating = True
    End Sub
    [/CODE]
    Attached Files Attached Files
    Last edited by Rosco81; 02-22-2010 at 11:16 AM. Reason: to add codes

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1