+ Reply to Thread
Results 1 to 8 of 8

Thread: Macro to filter the same column regardless of changes

  1. #1
    Forum Contributor
    Join Date
    11-26-2008
    Location
    UK
    Posts
    103

    Macro to filter the same column regardless of changes

    Hi,

    I’ve recorded a macro to run a filter (as below) it and it works fine. The only problem I have is that sometimes a column will be added, or possibly removed from the sheet - meaning the filter won’t always work. Is there a way to make sure the filter always does what it does on the same column, regardless of any changes to the column's location?

    Sub late()
    '
    ' late Macro
    '
    
    '
        Range("J8:J11").Select
        Selection.AutoFilter
        ActiveSheet.Range("$B$8:$J$94").AutoFilter Field:=9, Criteria1:="=*late*", _
            Operator:=xlAnd
    End Sub
    Last edited by Barking_Mad; 07-21-2010 at 08:17 AM.

  2. #2
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,151

    Re: Macro to filter the same column regardless of changes

    Hi Barking_Mad,
    Possibly if you know the field title eg if the coulmn title is "Two"
    x=application.worksheetfunction.MATCH("Two",A7:z7,0)
    ActiveSheet.Range("$B$8:$J$94").AutoFilter Field:=x, Criteria1:="=*......
    Last edited by pike; 07-16-2010 at 07:33 AM.
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

  3. #3
    Forum Contributor
    Join Date
    11-26-2008
    Location
    UK
    Posts
    103

    Re: Macro to filter the same column regardless of changes

    Hi Pike,

    Thanks, but im having trouble getting the compiler to accept the code....im crap at VBA so please excuse..It's giving me a synstax error

    x=application.worksheetfunction.MATCH("Two", A7:z7,0)
    Additionally, should i delete

    Range("J8:J11").Select
        Selection.AutoFilter
    from the original code?

  4. #4
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,151

    Re: Macro to filter the same column regardless of changes

    Hi Barking_Mad
    Could you attach the workbook?
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

  5. #5
    Forum Contributor
    Join Date
    11-26-2008
    Location
    UK
    Posts
    103

    Re: Macro to filter the same column regardless of changes

    Here you go, ive taken bits and pieces of sensitive info out, but the column i want to filter is the ANNOTATIONS one on the end of the sheet, namely anything with LATE in the column of cells..
    Attached Files Attached Files

  6. #6
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,151

    Re: Macro to filter the same column regardless of changes

    Dude try this...

    Option Explicit
    Sub tester()
       Dim xCount As Long
        xCount = Application.WorksheetFunction.Match("Annotations", Range("A8:z8"), 0)
        With Cells(8, xCount)
            ActiveSheet.Range("B8:" & Chr(xCount + 64) & "94").AutoFilter Field:=xCount - 1, Criteria1:="=*late*"
        End With
    End Sub
    Last edited by pike; 07-16-2010 at 08:07 PM. Reason: xcount added
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

  7. #7
    Forum Guru pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2010
    Posts
    5,151

    Re: Macro to filter the same column regardless of changes

    Barkingmad
    also removed the merged cells
    Attached Files Attached Files
    regards pike

    If the solution helped please donate
    here to the RSPCA

    Sites worth visiting;

    J&R Solutions - royUK

    AJP Excel Information - Andy Pope

    Spreadsheet Toolbox

    VBA for smarties - snb

  8. #8
    Forum Contributor
    Join Date
    11-26-2008
    Location
    UK
    Posts
    103

    Re: Macro to filter the same column regardless of changes

    Thanks Pike

+ 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