+ Reply to Thread
Results 1 to 11 of 11

Autofilter macro

Hybrid View

  1. #1
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    Autofilter macro

    Hi Everyone,

    I have spreadsheet that has two tabs. One the first tab, entitled 'Dashboard', I have the text along the lines of 'Search for a name here'

    On the second tab, entitled 'Team List' I have the details of the team, eg name, address, emergency contact etc.

    I recorded the following macro:-

    Sub Search()
    '
    ' Search
    '
    
    '
        
        Sheets("Dashboard").Select
        Range("B4").Select
        Selection.Copy
        Sheets("Team List").Select
        ActiveSheet.Range("$A$1:$X$397").AutoFilter Field:=13, Criteria1:=Array( _
            "Bob Jones", _
            "David Jones", _
            "Sally Jones"), Operator:=xlFilterValues
            
    End Sub
    What I want to do is type in 'Jones' into the search area, the marco to copy and paste that ino the autofilter, and all of the names with 'Jones' be displayed.

    The macro above works great for this, but if I enter 'Smith' it still displays the details for 'Jones'.

    I think its because of the "Bob Jones", - "David Jones",- "Sally Jones") bit, but I dont know what the generic script for this would be.

    Can anyone help?

    Thanks
    Alex

  2. #2
    Forum Expert
    Join Date
    12-15-2009
    Location
    Chicago, IL
    MS-Off Ver
    Microsoft Office 365
    Posts
    3,177

    Re: Autofilter macro

    Can you attach your file?

  3. #3
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Autofilter macro

    Alex,

    Something like this should work for you:
    Sub Search()
        
        With Sheets("Team List").Range("M1", Sheets("Team List").Cells(Rows.Count, "M").End(xlUp))
            .AutoFilter 1, "*" & Sheets("Dashboard").Range("B4").Text & "*"
        End With
            
    End Sub
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  4. #4
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Autofilter macro

    Hi, Thank you for your help so far, unfortunately the code supplied by Tigeravatar didnt work work, although your help is appreciated. The debugger wouldnt go past the range section but Im not sure why not.

    I have attached a VERY basic version of my file. Please dont think that my excel training has been that simple!

    Thanks for your help
    Alex
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Autofilter macro

    hi Alex, if it is to search on column B only it can be done this way
    Attached Files Attached Files
    Last edited by watersev; 12-07-2012 at 05:26 AM.

  6. #6
    Registered User
    Join Date
    11-27-2012
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Autofilter macro

    Hi Watersev, this is fantastic and also answers my question! Just one thing, can I keep the hyperling associated with the original cell?
    thanks

  7. #7
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Autofilter macro

    Forum Rules
    2. Don't post a question in the thread of another member -- start your own. If you feel it's particularly relevant, provide a link to the other thread.
    Last edited by watersev; 12-10-2012 at 09:19 AM.

  8. #8
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Autofilter macro

    Hi,

    Thanks for all your help. Im sorry its taken so long to get back, Ive been away from work for a few days.

    The file attached above says that it corrupt when I try to open it. Am I doing something wrong when opening? Ive tried to save it first with the same result.

    Would you be able to post the code by any chance?

    Thanks again
    Alex

  9. #9
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Autofilter macro

    checked the attached file, works normally

    Private Sub test()
    
    Dim TL As Worksheet
    
    Application.ScreenUpdating = 0
    
    ActiveSheet.UsedRange.Offset(4).ClearContents
    
    If Range("b5") = "" Then Exit Sub
    
    Set TL = Sheets("Team List")
    If TL.Cells(Rows.Count, 1).End(xlUp).Row = 1 Then Exit Sub
    
    With TL.UsedRange
        
        TL.AutoFilterMode = 0
        .AutoFilter 2, "*" & Range("b5") & "*"
    
        If .Cells(Rows.Count, 2).End(xlUp).Row > 1 Then
            .Copy
            Range("b7").PasteSpecial xlPasteValues
        End If
        
        .AutoFilter
        Range("b7").Select
        
    End With
    
    End Sub

  10. #10
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Autofilter macro

    Wow! Thats fantastic!

    Thank you so much,
    Alex

  11. #11
    Registered User
    Join Date
    02-01-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2010
    Posts
    97

    Re: Autofilter macro

    Briliant, thanks Watersev. Ill try it straight away.

+ 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