Consulting

Results 1 to 10 of 10

Thread: Advance Filter using VBA

  1. #1

    Advance Filter using VBA

    I have data in column A,b,c its a huge list.
    Example :
    Sheet 1
    Headings.
    Let's say column A is System type like Desktop/Laptop.
    Column B is System model like ABC/XYZ/PQR.
    Column C has part # like 1234/5678/789/345...

    Requirements Sheet2.
    By default all column A,B,C should have all data as it is in Sheet1.

    Now if I click on any System type (column A), it should auto filter the list in Column B and C.
    If I click on any specific System Model(column B), Column 3 should display list corresponding to the selection

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Revised for successive filter.
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        If Target.Column < 4 And Target.Row > 4 And Target <> "" Then
            Cells(2, Target.Column) = Target
            Range("A4").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
            Range("A1").CurrentRegion, Unique:=False
            With Sheets(2)
                .Cells.ClearContents
                Range("A4").CurrentRegion.Copy .Cells(1, 1)
            End With
        Else
            Range("A2:C2").ClearContents
            On Error Resume Next
            ActiveSheet.ShowAllData
        End If
    End Sub
    Attached Files Attached Files
    Last edited by mdmackillop; 08-23-2017 at 05:40 AM. Reason: Edited to include copy
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    autofilter

    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
    
        If Intersect(Target, Cells(1).CurrentRegion) Is Nothing Then Exit Sub
        
        Sheets("Sheet2").Cells(1).CurrentRegion.ClearContents
         
        With Cells(1).CurrentRegion
            .AutoFilter Target.Column, Target.Value
            .Copy Sheets("Sheet2").Cells(1)
        End With
        
        Cancel = True
        
    End Sub

  4. #4
    Thanks for help.
    I am looking for something like this.
    Assume sheet 1 for data
    Sheet 2 will have the same column heading and everything will be displayed.
    But in sheet 2 if I click on any cell it should filter accordingly.
    File attached.
    Attached Files Attached Files

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You've made no comment on the proposed solutions posted.

    But in sheet 2 if I click on any cell it should filter accordingly.
    As you have sheet names, please use them. I assume Sheet 2 is Filter
    There are only headers in sheet Filter in your example so how can this work?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    The sheet you made was awesome
    How ever I want that the raw data should be in sheet 1 and sheet 2 should be the filter.
    I mean sheet 1 will be hidden, but users can use sheet2 for filtering data

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Put my code in your Filter sheet code module with layout as per my example.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Pasted the code didn't work, can you please help with an example file, if possible.

  9. #9
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Filter sheet module

    Option Explicit
    
    
    Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
         
          Unprotect
         
        If Intersect(Target, Cells(2).CurrentRegion) Is Nothing Then
            Sheets("Data").Cells(1).CurrentRegion.Copy Cells(2)
            AutoFilterMode = False
        Else
            Cells(2).CurrentRegion.AutoFilter Target.Column - 1, Target.Value
        End If
    
        Cancel = True
        Protect
         
    End Sub

    マナ

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If you remove row and column headers, make column A narrow and move your data location then not surprisingly it may go unnoticed that the code needs to be amended. For the future, keep your samples basic and fix the appearance once things are working.
    Attached Files Attached Files
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •