+ Reply to Thread
Results 1 to 4 of 4

Search for Text and displaying the results

Hybrid View

  1. #1
    Registered User
    Join Date
    04-18-2014
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    2

    Search for Text and displaying the results

    Hi,

    I'm trying to set up a system that searches for a string within a range of cells and displays all the results. So far I have been able to display only exact matches, but I'd like to display any matches within a string.

    I'm using this code which I found to search for text:
    Sub SearchForString()
    
        Dim LSearchRow As Integer
        Dim LCopyToRow As Integer
        Dim SearchTerm As String
                
        On Error GoTo Err_Execute
        
        Sheets("sheet1").Range("a" & 1 & ":e" & 10).ClearContents
        
        'Start search in row 4
        LSearchRow = 15
        
        'Start copying data to row 2 in Sheet2 (row counter variable)
        LCopyToRow = 2
        
        'Define Search term
        SearchTerm = InputBox("What would you like to search for?")
        
        While Len(Range("A" & CStr(LSearchRow)).Value) > 0
            
            'If value in column E = SearchTerm, copy entire row to Sheet2
            If Range("E" & CStr(LSearchRow)).Value = SearchTerm Then
           
                           
                'Select row in Sheet1 to copy
                Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
                Selection.Copy
                
                'Paste row into Sheet2 in next row
                Sheets("Sheet1").Select
                Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
                ActiveSheet.Paste
                
                'Move counter to next row
                LCopyToRow = LCopyToRow + 1
                
                'Go back to Sheet1 to continue searching
                Sheets("Sheet1").Select
                
            End If
            
            LSearchRow = LSearchRow + 1
            
        Wend
        
        'Position on cell A3
        Application.CutCopyMode = False
        Range("A3").Select
        
        MsgBox "All matching data has been copied."
        
        Exit Sub
        
    Err_Execute:
        MsgBox "An error occurred."
        
    End Sub
    I've been trying different ways to check for text within the selected field, but can't seem to get it to work.

    Any help would be appreciated, thanks

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

    Re: Search for Text and displaying the results

    Maybe try:

    Changing this line:

    Sub ymaster44()
    If Range("E" & CStr(LSearchRow)).Value = SearchTerm Then
    End Sub
    To one of these?

    Sub ymaster44()
    'If Range("E" & CStr(LSearchRow)).Value Like "*" & SearchTerm & "*" Then
    'Or
    'If SearchTerm Like "*" & Range("E" & CStr(LSearchRow)).Value & "*" then
    End Sub

  3. #3
    Registered User
    Join Date
    04-18-2014
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Search for Text and displaying the results

    That's perfect, thanks very much

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

    Re: Search for Text and displaying the results

    You're welcome. Glad to help out and thanks for the feedback.

+ 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] Search for Identifier in Column (different sheet) and Displaying Results in Specific Cells
    By Velocir in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-28-2013, 11:37 AM
  2. [SOLVED] Finding and Displaying data results from search criteria
    By winlam in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-25-2012, 11:25 PM
  3. displaying search results after searching 2 cbo's
    By ncaravela in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-31-2010, 08:19 AM
  4. Displaying Search results (filtering?)
    By Klok in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-04-2009, 06:45 AM
  5. Displaying text along with formula results
    By jesterhs in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-17-2005, 03:00 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