+ Reply to Thread
Results 1 to 6 of 6

Macro: filtering out min AND max value from a column

Hybrid View

  1. #1
    Registered User
    Join Date
    01-16-2014
    Location
    CA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Macro: filtering out min AND max value from a column

    Hello everyone, I'm Steve, new here. Came for excel guru expertise. Have a question about creating a macro that will filter out 2 (changing) values on a filter.

    I use a report that comes to me weekly with multiple tabs (always named the same) and I spend a lot of time formatting the report the same way each week to get the information out of it I need. Naturally I decided to create a macro to speed up the process and do a lot of it for me. Problem is, on these multiple tabs, the values I am looking at each week and filtering out are not the same week to week, or tab to tab.

    As it stands now, I apply a filter to row 1 (column headings), and then filter out the highest and the lowest values in a certain column. There is always a 1, followed by 2, 3, etc, all the way up into the hundreds or thousands. I always filter out 1 and whatever the highest value is. Since there is not a way to select min AND max, my current workaround is to use Number Filters and use the "between" function specifying greater than or equal to 2, and less than or equal to 100 since most of the time the value I am filtering out is not less than 100. But this is not foolproof and could potentially mess up my report should a less than 99 come back, for example. The other workaround I was looking into is conditional format that would use some sort of formula to color coordinate based off the determined max value on each tab, whatever it happens to be, and then filter out by color.

    I am not sure if this would be best route, or if my current workaround is the best solution? I haven't been able to find anything else that will work and am not necessarily an expert on all things excel to know the easiest way to get it done. That is why I am here asking for guidance. I was told there may be a way to insert an additional column with a combination of if formulas, but haven't gone that far yet.



    And then my follow-up question is if it is possible to build into the macro a way to copy and paste the contents of each tab into 1 tab at the end. I want all info (all formatted the same, same columns, same headings) from each tab copied over to a compiled list tab at the end, but I am not sure if this is possible. If it is, my second question is it possible to copy a certain format at a time? For example, if each tab contains red and green text/font, can I copy all red text to 1 tab, and green to a separate. I was trying to play with this today and copy all (from tab 1) and paste into the compiled view tab at the end, but was not able to figure out going back to tab 2, and pasting the contents under the already pasted tab 1 contents without overwriting it. I was attempting to do something like CTRL down to find the bottom value and then paste under it, but I may have messed it up. Not sure if there is some paste append option that I am missing.


    Sorry for the long read, but these are the 2 snags I am hitting in creating a macro that would save me a lot of time. I appreciate any assistance. Thank you for your time.

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

    Re: Macro: filtering out min AND max value from a column

    Here is example code to filter column A to exclude one and the max value.

    Sub Filter_Out_One_And_Max_Col_A()
        
        Dim lMax As Long
        
        lMax = Application.Max(Range("A:A"))
        
        Range("A:A").AutoFilter Field:=1, _
                                Criteria1:=">1", _
                                Operator:=xlAnd, _
                                Criteria2:="<" & lMax
                                
    End Sub


    follow-up question:
    How are the cell's font colored; Manually or using Conditional Formatting? They are not the same color properties for a cell. It may help to supply an example file and ask based on a specific data set.
    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
    Registered User
    Join Date
    01-16-2014
    Location
    CA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Macro: filtering out min AND max value from a column

    awesome, thank you. I will try that tomorrow at work. that is very helpful.

    what i had been doing was applying the filter to the columns, filtering out the 1 and max, selecting all and applying red font to those remaining, then unfiltering the sheet. if there is a way to apply the color to the desired rows (all but 1 and max), that could accomplish the same thing i am trying to do. it would actually be the rows who have an N/A in a separate column, and then the value of all except 1 and the max value. so i guess that could be something like an if and, but would require formulas somewhere.

  4. #4
    Registered User
    Join Date
    01-16-2014
    Location
    CA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Macro: filtering out min AND max value from a column

    let me ask one thing after glancing at that macro code....does this change anything if the values in said column appear more than once. for example....i do have multiple 1s as well as multiple of whatever the max value is. to shorten the list...i could appear as so...

    1
    1
    1
    2
    2
    3
    3
    3
    4
    4
    4
    4
    4
    4
    4
    5000
    5000
    5000
    5000

    kind of basic for my report, but gives an idea of what i am looking at. i would want to filter out 1 and 5000.

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

    Re: Macro: filtering out min AND max value from a column

    Yes it will filter out multiple rows containing 1 and multiple rows containing max.


    You wouldn't have to color the font to tag the filtered rows. That's an unnecessary step. VBA can copy just the visible filtered data

    Example:
    Sub Filter_Out_One_And_Max_Col_A()
        
        Dim lMax As Long
        
        ActiveSheet.AutoFilterMode = False
        
        lMax = Application.Max(Range("A:A"))
        
        With Range("A:B")   'Flter data in columns A and B
        
            'Filter column A to exclude one and max value
            .AutoFilter Field:=1, _
                        Criteria1:=">1", _
                        Operator:=xlAnd, _
                        Criteria2:="<" & lMax
                        
            'Filter column B for "N/A"
            .AutoFilter Field:=2, Criteria1:="N/A"
            
            'Copy rows of Filtered visible data
            'Paste to sheet 2
            Range("A2", Range("A" & Rows.Count).End(xlUp)).EntireRow.Copy _
                Destination:=Sheets("Sheet2").Range("A1")
                                
        End With
        
        ActiveSheet.AutoFilterMode = False
                                
    End Sub
    Again, it would be much easier for me if you provided a specific example data file instead of referring to "a separate column".
    Last edited by AlphaFrog; 01-17-2014 at 12:37 AM.

  6. #6
    Registered User
    Join Date
    01-16-2014
    Location
    CA
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Macro: filtering out min AND max value from a column

    thank you for the help alpha. i did not bring a file home with me so i could not post an actual file. i will tonight if i have any trouble implementing these codes into the macro i am working on. i know it would be much easier for you to see the actual document. again, thanks for your help so far.

+ 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. macro for filtering A column and summing up respective cells in B column
    By rajkumarmp in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-27-2013, 11:12 AM
  2. [SOLVED] Need Help with Macro, filtering one column, and filldown value to last row of a new column
    By mDevel in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 11-08-2013, 01:04 PM
  3. [SOLVED] trouble filtering a list. Why isn't column filtering?
    By Anne Troy in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-06-2005, 04:05 AM
  4. [SOLVED] trouble filtering a list. Why isn't column filtering?-number of criteria
    By Pat in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-18-2005, 11:05 AM

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