+ Reply to Thread
Results 1 to 7 of 7

Help creating macro that applies filters to area relative to active cell

  1. #1
    Registered User
    Join Date
    07-11-2020
    Location
    Guildford, England
    MS-Off Ver
    Professional plus 2016
    Posts
    9

    Help creating macro that applies filters to area relative to active cell

    Very much a macro noob here, spent all day on this problem and can't seem to find a solution to it
    I've got the following spreadsheet
    graph picture.PNG
    Now what I want to do is create a macro which removes all the white space from an individual column, showing only the numbers other than zero and their corresponding dates
    The final table after doing this for one column looks like this
    Filter table example.PNG
    I did this by simply unticking 'blank' when I opened the filter menu and it somehow worked
    Now however, I want to make it so that whenever I click on one of the cells on the 'data' row and press a macro button, it will do the exact same to that column of data, I've tried making this a macro by performing this process with 'use relative references' on, however excel always writes the VBA code for it as -
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.Range("$F$9:$F$1004").AutoFilter Field:=1, Criteria1:="="
    ActiveSheet.Range("$F$9:$F$1004").AutoFilter Field:=1, Criteria1:="<>"
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    ActiveCell.Offset(7, 5).Range("A1").Select
    ActiveSheet.Paste
    ActiveCell.Select
    End Sub
    So the big problem that I can see right now is that the range for the active sheet thing is in absolute terms, while everything else is in relative terms, I'm not entirely sure how to change this or what I should be doing differently
    The closest thing I've thought of is the particularly hamfisted approach of changing the filter code to -
    ActiveSheet.Range(ActiveCell.Offset(1, 0).Range("A1:A1000")).AutoFilter Field:=1, Criteria1:="="
    ActiveSheet.Range(ActiveCell.Offset(1, 0).Range("A1:A1000")).AutoFilter Field:=1, Criteria1:="<>"
    However this isn't working and debugger says that these lines are the reason why
    Anyone have any idea what I could do here?
    Apologies if this is something you learn to fix in lesson one of vba academy, I'm rather clueless as to what I'm doing, never coded before or anything even remotely similar.
    Thank you very much

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Help creating macro that applies filters to area relative to active cell

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,673

    Re: Help creating macro that applies filters to area relative to active cell

    Is the outcome of this what you have in mind?
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    07-11-2020
    Location
    Guildford, England
    MS-Off Ver
    Professional plus 2016
    Posts
    9

    Re: Help creating macro that applies filters to area relative to active cell

    Quote Originally Posted by jolivanes View Post
    Is the outcome of this what you have in mind?
    Please Login or Register  to view this content.
    This gives the result I want for one column, however I can only apply it to data from that single column, I'm looking for something where I can click on the 'data' row on whichever column I want to apply that filter to, and then click a macro button which applies that filter to everything below that active cell, also I need something which is reversible so that when I'm done with that column I can simply delete the filter and get back the data I had to begin with.
    Thanks for having a look, this has really stumped me!
    Last edited by Cubictulip; 07-12-2020 at 12:31 PM.

  5. #5
    Registered User
    Join Date
    07-11-2020
    Location
    Guildford, England
    MS-Off Ver
    Professional plus 2016
    Posts
    9

    Re: Help creating macro that applies filters to area relative to active cell

    Quote Originally Posted by AlphaFrog View Post
    Please Login or Register  to view this content.
    This code works for that column, but there's a few issues
    It doesn't work for the second column, so for example it works for column F, but if you have another column at J for example it doesn't perform the same task, it tells me to 'select a cell within the data range'.
    Also, it deletes everything on that sheet when you run it, only leaving the relevant dates and associated numbers, there's some data above my screenshot that I probably should have shown but I need to keep that there, I only want the macro to alter stuff below the 'date' row
    Also, it isn't reversible, for some reason I can't even ctrl + z my way out of it, I need to be able to click a button (for example adding a bit onto the macro that automatically removes all filters on the 'date' row after it's finished giving this data so the sheet returns to how it was before the button was pressed.
    Thanks for helping, I don't really know what I'm doing here so this is really appreciated!

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Help creating macro that applies filters to area relative to active cell

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    07-11-2020
    Location
    Guildford, England
    MS-Off Ver
    Professional plus 2016
    Posts
    9

    Re: Help creating macro that applies filters to area relative to active cell

    Quote Originally Posted by AlphaFrog View Post
    Please Login or Register  to view this content.
    Thank you very much for this, it works extremely well!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Hiding rows relative to the active cell
    By joeywizzle in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-20-2015, 03:48 PM
  2. Paste issue in active cell with Macro using relative reference
    By pdjh23 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-08-2013, 03:38 AM
  3. Macro to expand all the filters on the active worksheet.
    By Kimston in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-16-2012, 03:18 PM
  4. Replies: 0
    Last Post: 02-28-2012, 09:47 AM
  5. Copy cells relative to active cell
    By Grouty in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 12-20-2011, 06:32 AM
  6. Relative Cell References when creating VBA Macro for Chart
    By BHcristo in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 12-04-2010, 10:13 AM
  7. Change Active Cell but not it's relative position
    By johngombola in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-24-2009, 05:39 AM

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.6.0 RC 1