+ Reply to Thread
Results 1 to 11 of 11

Thread: Link Autofilter to cell

  1. #1
    iskandak
    Guest

    Link Autofilter to cell

    Hi,

    I linked cell A1 to autofilter in A3, A1 get data from drop down list in C1.
    the problem every time when I change value in C1, I need to click A1 than click F2 to work. see the attached file.

    Your help is needed.

    Thanks
    Attached Files Attached Files
    Last edited by iskandak; 07-16-2011 at 06:34 AM.

  2. #2
    Forum Contributor
    Join Date
    03-10-2011
    Location
    Manchester, England
    MS-Off Ver
    Excel 2003
    Posts
    132

    Re: Link Autofilter to cell

    Change the code to -

    Private Sub Worksheet_Change(ByVal Target As Range)
    Const WS_RANGE As String = "C1"     '<== change to suit
    
        On Error GoTo ws_exit
        Application.EnableEvents = False
        If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
            Me.Range("A3").AutoFilter field:=1, Criteria1:=Target.Value
       
        End If
    
    ws_exit:
        Application.EnableEvents = True
    End Sub
    You dont need cell A1 then and the change event is recognised.

    Dave H

  3. #3
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Link Autofilter to cell

    You don't need the A1 reference, this will trigger properly off of C1. I added a "Show All" option that will unfilter the table, too. You can also delete the C1 value so it's blank to also unfilter the table.
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  4. #4
    iskandak
    Guest

    Re: Link Autofilter to cell

    Thank you for your replay,

    What about if you want to get drop down from other page like A1 on Sheet 2. So that when you change the value on A1 on Sheet 2, the autofilter will change accordingly on Sheet .

    The main thing in this issue is link the autofilter to other link or sheets.

    See the attached file.

    Thanks
    Attached Files Attached Files

  5. #5
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Link Autofilter to cell

    It's still pretty much the same macro, you just move the event macro into the Sheet2 module, then edit the code to apply the filter commands on Sheet1. Like so...
    Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo ws_exit
        
        Application.EnableEvents = False
        
        If Not Intersect(Target, [A1]) Is Nothing Then
            Select Case Target.Value
                Case "Show All", ""
                    Sheets("Sheet1").AutoFilterMode = False
                Case Else
                    Sheets("Sheet1").Range("A1").AutoFilter 1, Target.Value
            End Select
            
        End If
    
    ws_exit:
        Application.EnableEvents = True
    End Sub
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  6. #6
    iskandak
    Guest

    Re: Link Autofilter to cell

    Thank a lot for your cooperation.

    Solved.

  7. #7
    iskandak
    Guest

    Re: Link Autofilter to cell

    Hi,

    Autofilter not work with protect sheet.

    See the attached file.

    Thanks
    Attached Files Attached Files

  8. #8
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Link Autofilter to cell

    The sheet you uploaded is working fine. Sheet2 is protected but the cell with the drop down is not protected thus you can still use the drop down, and that's all it takes to trigger the Autofilter on Sheet1 which is not protected.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  9. #9
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Link Autofilter to cell

    Ah, I see what you mean. The simplest way to resolve running macro on protected sheets is to FIX the protection when then workbook opens so that you CAN run macro on the protected sheet. There is one parameter you can set when protecting a sheet called UserInterfaceOnly:=True that is added to a protection command so the protection no longer applies to VBA, only the the user. Macros are then free to do what they will on the protected sheet.

    Simplest.... put this macro into a regular module1:
    Sub ProtectData()
        Sheets("Sheet1").Protect _
            DrawingObjects:=False, _
            Contents:=True, _
            Scenarios:=False, _
            AllowFiltering:=True, _
            UserInterfaceOnly:=True
    End Sub

    Then put this macro into the ThisWorkbook module:
    Private Sub Workbook_Open()
        ProtectData
    End Sub

    The flag UserInterfaceOnly does not persist when the workbook is closed, it must be reactivated each time you open the workbook. So, this will do that. Also, if you unprotect sheet1 to work on it, reprotect it using the macro ProtectData so that flag gets set properly again.
    Attached Files Attached Files
    Last edited by JBeaucaire; 07-14-2011 at 04:00 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  10. #10
    iskandak
    Guest

    Re: Link Autofilter to cell

    Thank a lot

    Solved.

  11. #11
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Link Autofilter to cell

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0