+ Reply to Thread
Results 1 to 5 of 5

Search specific column and then copy to new sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    10-24-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    32

    Arrow Search specific column and then copy to new sheet

    Hello again,

    I need a way so that I can press a button and then a box will appear and allow me to type a search word. It will then scan a particular column if it contains that word and then it will copy the entire row and paste it into a new sheet.

    It must be able to handle multiple search terms.

    So: If I have multiple names like "John Smith" and then 20 rows down I have John Smith again and then another 10 times it has John Smith in that column, then it will copy the entire row in which john smith is within, (All of them) and paste them into a new sheet.

    It should copy the entire row including the columns before, because the column where it is searching for will mostly likely not be column A.

    Thank's again

    ~JSN
    Last edited by PowerZ; 10-25-2010 at 10:10 AM.

  2. #2
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Search specific column and then copy to new sheet

    Give this a try. You did not specify which column to search in so this code assumes you want to search in the active cell column. This code uses filtering to copy all (exact) occurrences of the search term to Sheet2.

    Option Explicit
    
    Sub Find_and_Copy()
    
        Dim lastrow As Long, lCol As Long, sCriteria As String
        
        lCol = ActiveCell.Column
        lastrow = Cells(Rows.Count, lCol).End(xlUp).Row
        
        sCriteria = Application.InputBox("Enter a search term", Type:=2)
        
        If sCriteria = vbNullString Then Exit Sub
            
        Application.ScreenUpdating = False
        
        With Sheet1
            .AutoFilterMode = False
            If WorksheetFunction.CountIf(.Range(.Cells(1, lCol), .Cells(lastrow, lCol)), sCriteria) < 1 Then
                MsgBox "The search term is not listed in the active cell column"
                Exit Sub
            End If
            .Range(.Cells(1, lCol), .Cells(lastrow, lCol)).AutoFilter field:=1, Criteria1:="=" & sCriteria
            .Range(.Cells(1, lCol), .Cells(lastrow, lCol)).SpecialCells(xlCellTypeVisible).Copy Sheet2.Cells(Rows.Count, "A").End(xlUp).Row + 1
            .AutoFilterMode = False
        End With
        
        Application.ScreenUpdating = True
    
    End Sub
    Last edited by Palmetto; 10-25-2010 at 07:10 AM.
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  3. #3
    Registered User
    Join Date
    10-24-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Search specific column and then copy to new sheet

    Okay, That code seems to create a pivot-table, instead of copying the results to a seconds sheet.

    Which actually work's better. So could you just modify that a little bit, and just delete the copying to the second sheet and make it so it will look in the "D" column. Also one last thing, to make a button which would reset the pivot table to show all.

    So that then, you can filter the results, and then see the row, and then reset it back to normal.

    Thank's for the quick response!

    ~JSN

  4. #4
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: Search specific column and then copy to new sheet

    The code above DOES NOT create a Pivot Table . As I stated - it applies filtering, which is not the same.

    Code below is revised per your last post to look only in column-D.

    Option Explicit
    
    Sub Find_and_Copy()
    
        Dim lastrow As Long, sCriteria As String
        
        lastrow = Cells(Rows.Count, "D").End(xlUp).Row
        
        sCriteria = Application.InputBox("Enter a search term", Type:=2)
        
        If sCriteria = vbNullString Then Exit Sub
            
        Application.ScreenUpdating = False
        
        With ActiveSheet
            .AutoFilterMode = False
            If WorksheetFunction.CountIf(.Range("D1:D" & lastrow), sCriteria) < 1 Then
                MsgBox "The search term is not listed in the column"
                Exit Sub
            End If
            .Range("D1:D" & lastrow).AutoFilter field:=1, Criteria1:="=" & sCriteria
        End With
        
        Application.ScreenUpdating = True
    
    End Sub

    Also one last thing, to make a button which would reset the pivot table to show all.
    You can turn the filter off via the menu. However, here is code which you can use to do this. Just create a button and assign this macro to the button.

    Sub Clear_Filter()
    
        With ActiveSheet
            .AutoFilterMode = False
        End With
    
    End Sub

  5. #5
    Registered User
    Join Date
    10-24-2010
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Search specific column and then copy to new sheet

    Thank you for this. Sorry about that, I thought it was a Pivot-Table!

    Thank's for the quick responses and great help!

    ~JSN

+ Reply to Thread

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