+ Reply to Thread
Results 1 to 6 of 6

Search box and search button help

Hybrid View

  1. #1
    Registered User
    Join Date
    09-03-2013
    Location
    Monterey, CA
    MS-Off Ver
    Excel 2010
    Posts
    3

    Search box and search button help

    Hello! I am new to the forum - have lurked around before, but just joined and need help with a macro for a search box. Attached is a sample of my database and an example (pulled from another thread on here) that I wish to copy. I have searched these forums and played with code that others recommended for other people, but have not been successful in adapting them to my needs. Any and all help is appreciated.

    In my database I would like a search box with a command button similar to the one in the attached "example database to copy." However, I do not want the search option or the results to be on a separate page. I would simply like them to be displayed on the same page underneath the same headings with all other data removed. Then, when you clear the search box all data returns.

    Users need to be able to search the entire database (ie, not limited to one column, but whatever word they search for could come from any column). Results should appear when any "part" of the search is found. Last, this database is updated and added to monthly, and it would be great if the "ranges" covered allowed me to add additional data without messing up the code/search function.

    Thank you for any help.

    Jason

  2. #2
    Registered User
    Join Date
    06-18-2013
    Location
    China
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: Search box and search button help

    Hello JKolya,

    I think the example I am sending you will meet most of your requirements.



    Best Regards,
    Toloza.
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    09-03-2013
    Location
    Monterey, CA
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Search box and search button help

    Toloza,

    Thank you for the reply and effort, however; my database is the other one - "Sample Database for search box option." The one titled "Example database to copy," is just to show a sample of the type of search function I am looking for.

    I tried to play with the one you sent, but the search does not seem to reply with the correct results. For example, searching "belt," brings three replies, but two of them do not contain the "belt." I am not sure why. Then when I delete the word, the data does not come back. Not sure what to do.

    J

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Search box and search button help

    Here are two macros you could assign to buttons. One to search for the prompted search term and the other to reset and show all the data again.

    Sub Find_All()
        Dim Found As Range, rngAll As Range, FirstFound As String, strSearchTerm As String
        
        strSearchTerm = Application.InputBox("Enter the search term.", "Search Term", Type:=2)
        If strSearchTerm = "False" Then Exit Sub    'User canceled
        
        Rows.Hidden = False
        Set Found = Cells.Find(What:=strSearchTerm, _
                               LookIn:=xlValues, _
                               LookAt:=xlPart, _
                               SearchOrder:=xlByRows, _
                               SearchDirection:=xlNext, _
                               MatchCase:=False)
                               
        If Not Found Is Nothing Then
            FirstFound = Found.Address
            Set rngAll = Found
            Do
                Set Found = Cells.FindNext(After:=Found)
                If Found.Address <> FirstFound Then Set rngAll = Union(rngAll, Found)
            Loop While Found.Address <> FirstFound
            
            Application.ScreenUpdating = False
                rngAll.Select
                Set rngAll = Union(rngAll, Rows("1:4")) 'Header rows
                ActiveSheet.UsedRange.Rows.Hidden = True
                rngAll.EntireRow.Hidden = False
            Application.ScreenUpdating = True
            
        Else
            MsgBox "No match found for '" & strSearchTerm & "'.", vbExclamation, "No Match Found"
        End If
        
    End Sub
        
    Sub Show_All()
        Rows.Hidden = False
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Search box and search button help

    Maybe:

    Sub JKolya()
    For Each cell In Sheets("Data").Range("B2:B" & Sheets("Data").UsedRange.Rows.count + 1)
        If cell.Value Like "*" & Sheets("Search").Range("B2").Value & "*" Then
            cell.Offset(, -1).Copy Sheets("Search").Range("A" & Rows.count).End(3)(2)
            cell.Copy Sheets("Search").Range("B" & Rows.count).End(3)(2)
        End If
    Next cell
    End Sub

  6. #6
    Registered User
    Join Date
    09-03-2013
    Location
    Monterey, CA
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Search box and search button help

    AlphaFrog,

    That worked perfectly. Thank you so much for taking the time.

    J

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro/s to create Search Bar to filter to search terms entered activated by Command Button
    By JasonRay in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-08-2013, 03:44 PM
  2. Need search button and update button vba codes
    By mayurpatil in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-09-2013, 09:36 AM
  3. Search Button
    By sdsliscool in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-21-2011, 06:21 PM
  4. Button to search
    By rmarin51 in forum Excel General
    Replies: 6
    Last Post: 10-19-2006, 07:03 PM
  5. [SOLVED] Button to allow a search
    By Fred in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-10-2005, 06:06 PM

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