+ Reply to Thread
Results 1 to 20 of 20

Help With A Formula To Calculate Only Filtered Data?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-05-2010
    Location
    CA
    MS-Off Ver
    Excel 365
    Posts
    539

    Question Help With A Formula To Calculate Only Filtered Data?

    Hello,
    This is a continuation of a thread I started a few days ago asking for assistance with creating a formula to transfer a portion of data from one tab to another in the same worksheet. The worksheet consists of two tabs, Original and Sort. I wanted to import a certain portion of data from the Original tab to the Sort tab and received help with a formula that did that exactly. Now I’m seeking help with the next step with what I am trying to do and that’s filter the data and calculate the filtered results.

    Phase 2
    First question I have regarding what I am trying to do is find out if it’s possible for Excel to calculate data that has been filtered using the filtered function. I want to be able to use the Excel’s filter to select a certain criteria of data and have a formula give me specific information about that filter and if I make changes it will give me information based upon the new filtering as well and so on. If it is indeed possible the following is what I would like to do, if not I would have to find other means. After the selected data has been transferred to the Sort tab I then what to filter the data between the columns with “Start” and “Finish” above them and have formulas that calculate results based upon those filters.

    As in the example below:
    The cell “I9” has been sorted to show only the value of “6” that appear in all corresponding rows. Those are the only rows that would be active and selected based upon the criteria entered.

    After sorting, I then want a formula that list all the values that are showing in the “N9” (Values) and the number of times that a value appears and its percentage of the total number of values based on what remains after filtering in cell “O9” (Total: #/%). The tabulation will be done for all columns in the “Start” to “Finish” radius.

    Starting with column “H”, in cell “H9”, the formula in “N10” would list all of the values listed in the column after the filtering of the value 6 in column “I”. In column “H” the only value listed is 1 and it appears three times. Since it’s the only value that means it is 100% of the values in that column, so in “O10” the result from the formula should be 3/100%. I would require a formula that could do that for all of the columns from “Start” to “Finish” in the “Value” column for each factor

    I’m fine with the inclusion of helper columns or any additional formulas that are needed to achieve what I am trying to accomplish but would prefer a formula over VBA due to my lack of knowledge.

    2A.jpg

    Final note:
    Please see the attached spreadsheet for more information and a deeper breakdown of what I am trying to accomplish with a couple of Sort tab examples of how the data should be decided calculated based upon the Original tab. Also the actually spreadsheet contains much more data and factors. So I need a solution that can calculate the formulations quickly. Everything in the attached spreadsheet is calculated and entered by hand so it may contain errors. Hopefully my breakdown of what I require is sufficient explanation. Ignore the portions in red for they are areas to be formulated later.

    Thank you for any and all help you can provide it would be greatly appreciated. Please feel free to ask any and all questions.
    Attached Files Attached Files

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    91,077

    Re: Help With A Formula To Calculate Only Filtered Data?

    Just to clarify: there is no relevant detail in the other thread to which the OP refers, in which the OP merely asks if it is possible to perform calculations on filtered data. This is not a continuation of that thread, therefore, and no link to it is therefore necessary.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Contributor
    Join Date
    10-05-2010
    Location
    CA
    MS-Off Ver
    Excel 365
    Posts
    539

    Re: Help With A Formula To Calculate Only Filtered Data?

    Quote Originally Posted by AliGW View Post
    Just to clarify: there is no relevant detail in the other thread to which the OP refers, in which the OP merely asks if it is possible to perform calculations on filtered data. This is not a continuation of that thread, therefore, and no link to it is therefore necessary.
    Exactly, in the previous thread I was merely asking if it was possible. After determining that it was I created a new thread that described what I was seeking in exact detail. Thank you for clarifying that.

  4. #4
    Forum Contributor
    Join Date
    10-05-2010
    Location
    CA
    MS-Off Ver
    Excel 365
    Posts
    539

    Re: Help With A Formula To Calculate Only Filtered Data?

    I decided to breakdown what I am looking for into even smaller parts, starting with a listing of values that remain after filtering the data.

    See example below:
    Between columns “F” and “J” are the start and finish points for the columns that will be filtered. Columns “K” to “O” are where the formulas representing each column “F” to “J” are. In the example column “H” is filtered to show the value of “2” leaving only the rows that contain that value. In columns “K” to “O” I want a formula to list the values that remain for each column but not listing repeats, so as in column “F” there are eight 1s and one 5 so in column “K” the formula would only list one 1 and one 5. Keeping the values in numerical of alphabetical order. This would continue for all the columns.

    2C.jpg

    See the attached spreadsheet for a better understanding. Thank you any help you can provide.
    Attached Files Attached Files
    Last edited by artiststevens; 12-27-2016 at 06:50 PM.

  5. #5
    Forum Contributor
    Join Date
    10-05-2010
    Location
    CA
    MS-Off Ver
    Excel 365
    Posts
    539

    Re: Help With A Formula To Calculate Only Filtered Data?

    Any further help that could be provided would be greatly appreciated. Or even give me any idea of the direction I should head in. Thank you

  6. #6
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,908

    Re: Help With A Formula To Calculate Only Filtered Data?

    This might point you in the right direction: currently haven't time to follow through myself.


    http://blog.contextures.com/archives...filtered-list/

  7. #7
    Forum Contributor
    Join Date
    10-05-2010
    Location
    CA
    MS-Off Ver
    Excel 365
    Posts
    539

    Re: Help With A Formula To Calculate Only Filtered Data?

    Thank you John I'll check it out.

  8. #8
    Forum Contributor
    Join Date
    10-05-2010
    Location
    CA
    MS-Off Ver
    Excel 365
    Posts
    539

    Re: Help With A Formula To Calculate Only Filtered Data?

    I attempted to figure this out myself using the link provided by John but still having trouble. Any further help would be appreciated.

  9. #9
    Forum Expert avk's Avatar
    Join Date
    07-12-2007
    Location
    India
    MS-Off Ver
    Microsoft Office 2013
    Posts
    3,279

    Re: Help With A Formula To Calculate Only Filtered Data?

    Are you required column "F" sum is "13", if yes then use subtotal formula.


    atul


    If my answer (or that of other members) has helped you, please say "Thanks" by clicking the Add Reputation button at the foot of one of their posts.

    Also: if your problem is solved, please take the time to mark your thread as SOLVED by going to the top of your first post, selecting "Thread Tools" and then "Mark thread as solved".

  10. #10
    Forum Contributor
    Join Date
    10-05-2010
    Location
    CA
    MS-Off Ver
    Excel 365
    Posts
    539

    Re: Help With A Formula To Calculate Only Filtered Data?

    I appreciate the input AVK but that's not something I require.

  11. #11
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,908

    Re: Help With A Formula To Calculate Only Filtered Data?

    I have looked at this again and have concluded that what you requires cannot be done with formulae as determining the row numbers of the filtered data is the problem.

    For example in column K the values you require are in rows 5 and 11 whereas normally we expect to put the results in contiguous rows.

  12. #12
    Forum Expert avk's Avatar
    Join Date
    07-12-2007
    Location
    India
    MS-Off Ver
    Microsoft Office 2013
    Posts
    3,279

    Re: Help With A Formula To Calculate Only Filtered Data?

    I got it your requirement. (outpur within filter data only)
    >In column "K" : You want data from column "F" (extract only unique values)
    >In column "L" : You want data from column G" (using sort function)
    >In column "M" : You want data from column "H" (extract only unique values)
    >Not know regarding criteria in column "N" & "O" data.
    This is not done by formula. I think this can be done by vba micro. (I am not familiar with vba)

  13. #13
    Forum Contributor
    Join Date
    10-05-2010
    Location
    CA
    MS-Off Ver
    Excel 365
    Posts
    539

    Re: Help With A Formula To Calculate Only Filtered Data?

    You are somewhat correct. (Yes the output within filter data only)
    >In column "K" : I want data from column "F" (extract only unique values)
    >In column "L" : I want data from column G" (extract only unique values)
    >In column "M" : I want data from column "H" (extract only unique values)
    >In column "N" : I want data from column "H" (extract only unique values)
    >In column "O" : I want data from column "H" (extract only unique values)

    All of that is based upon the sorting of any column between "F" and "J".


    So you think VBA is the only solution? Probably the easiest as well... Hmmm... Ok


    Thank you again AVK

  14. #14
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,908

    Re: Help With A Formula To Calculate Only Filtered Data?

    Sub Filter_it()
    
    Dim i As Integer
    Dim lr As Long
    Dim keyrng As Range
    
    Application.ScreenUpdating = False
    
       lr = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
    
        Range("A3:O" & lr).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
            "K1:K2"), CopyToRange:=Range("AA3:AO" & lr), Unique:=False
            
        For i = 1 To 6
    
            Set keyrng = Range(Cells(4, 36 + i), Cells(4, 36 + i))
        '
        ' Remove Duplicates
        '
            Range(Cells(3, 36 + i), Cells(lr, 36 + i)).Select
            ActiveSheet.Range(Cells(3, 36 + i), Cells(lr, 36 + i)).RemoveDuplicates Columns:=1, Header:= _
                xlYes
         '
         '  Sort
         '
            ActiveSheet.Sort.SortFields.Clear
            ActiveSheet.Sort.SortFields.Add Key:=keyrng, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
                xlSortTextAsNumbers
            With ActiveSheet.Sort
                .SetRange Range(Cells(4, 36 + i), Cells(lr, 36 + i))
                .Header = xlNo
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
            
        Next i
        
        Application.ScreenUpdating = True
        
        Range("AA1").Select
        
    End Sub

    Filtered data is copied to AA3 (across and down).

    Duplicates removed from columns AK to AO and then sorted.

    Criteria for filter are in K1:K2:

    K1= Filter Column heading,

    K2= filter value
    Attached Files Attached Files
    Last edited by JohnTopley; 01-17-2017 at 09:12 AM.

  15. #15
    Forum Contributor
    Join Date
    10-05-2010
    Location
    CA
    MS-Off Ver
    Excel 365
    Posts
    539

    Question Re: Help With A Formula To Calculate Only Filtered Data?

    Wow thank you very much John! That is exactly what I was looking for. I greatly appreciate it. It runs and flows seamlessly.

    I do have a couple of questions and wonder if a few additions are possible. I have toyed with code myself but have been unable to make the changes and get the "Run Code Error"

    1.) Is it possible to enter multiple values in "K2", separating them with a coma or possible placing them "L2" through "O2". For example, have the Macro filtering for both "2" and "4" in the selected Criteria column of M.A. P. Day.

    2.) After filtering, I would then like to list number of times that a value appears and its percentage of the total number of values based on what remains after filtering in cell.

    For example, when filtering for criteria “M.A. P. Day” (#2) starting with column “AL” the values listed in the column after the filtering is “1” and “5”. With there being seven instances of “1” and one instance of “5”, so the result in “AM4” and “AM5” would be (7/87.5% and 1/12.5%)

    The final step breaks down the “AM4” and “AM5” results (7/87.5% and 1/12.5%) even further by encompassing the data in column “AG”. I need the code to calculate in the columns “AN” – “AR” (P:#/%, L:#/%, B:#/%, and WP:#/%) to show how many times a given value appears from column “AM” (Total:#/%). In the example, “AN4” (7/87.5%) would then show the number of times and percentages for P, L, B and WP from “AG”. The result being “AO4” – “AR4” (4/57.1%, 3/42.9%, 0/0%, 0/0%). The same would be done for “AM5”. The result being “AO5” – “AR5” (0/0%, 0/0%, 1/100%, 0/0%).

    filter_2.jpg

    Once again, thank you again John for all of the time and effort you put into making this work for me. I greatly appreciate it and if what I am asking for additionally is too much or not possible I still greatly appreciate what you did. Thank you.
    Attached Files Attached Files

  16. #16
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,908

    Re: Help With A Formula To Calculate Only Filtered Data?

    Re 1.) How many (max) values are you likely to choose? Using Advanced Filter, the values have to placed in the same column so they would go in L2, L3 etc.

    And do you want to filter on more than one field at a time?

    Why add column F which is just a repeat of E ? You don't need both.
    Last edited by JohnTopley; 01-19-2017 at 02:31 AM.

  17. #17
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,908

    Re: Help With A Formula To Calculate Only Filtered Data?

    See the atatched:

    Sub Filter_it()
    
    Dim i As Integer
    Dim lr As Long
    Dim keyrng As Range
    
    Application.ScreenUpdating = False
    
    Worksheets("Filtered").Range("A1:ZZ1000").ClearContents
    
    Worksheets("Filter_Formula").Activate
    
       lr = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
    
        Range("A4:O" & lr).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
            "Crit"), CopyToRange:=Range("Filter_Start"), Unique:=False
            
        Worksheets("Filtered").Activate
            
        For i = 11 To 15
        
           lr = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
    
            Set keyrng = Range(Cells(2, i), Cells(2, i))
        '
        ' Remove Duplicates
        '
            Range(Cells(1, i), Cells(lr, i)).Select
            ActiveSheet.Range(Cells(1, i), Cells(lr, i)).RemoveDuplicates Columns:=1, Header:= _
                xlYes
         '
         '  Sort
         '
            ActiveSheet.Sort.SortFields.Clear
            ActiveSheet.Sort.SortFields.Add Key:=keyrng, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
                xlSortTextAsNumbers
            With ActiveSheet.Sort
                .SetRange Range(Cells(2, i), Cells(lr, i))
                .Header = xlNo
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
            
        Next i
        
        Insert_Columns
        
        Columns("K:AN").Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
        End With
        
        Application.ScreenUpdating = True
        
        Range("A1").Select
        
    End Sub
    Sub Insert_Columns()
    
       headings = Array(" ", "Total: #/%", "P: #/%", "L: #/%", "B: #/%", "WP: #/%")
       pl = Array(" ", "*", "P", "L", "B", "WP")
    
        Columns("L:P").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Columns("R:V").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Columns("X:AB").Select
         Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Columns("AD:AH").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        
        nfrow = Cells(Rows.Count, "A").End(xlUp).Row - 1
        
        ncc = 5
        
        For i = 11 To 35 Step 6
        
            ncc = ncc + 1
            
            For j = 1 To 5
                nn = i + j
                Cells(1, nn) = headings(j)
                Call Calc_NP(i, nn, ncc, nfrow, pl(j))
            Next j
            
            nlast = Cells(Rows.Count, i).End(xlUp).Row
            
            For k = 2 To nlast
             pct = Cells(k, i + 1) / (nfrow - 1)
             Cells(k, i + 1) = Cells(k, i + 1) & "/" & Format(pct, "#0.0%")
            Next k
            
        Next i
        
    End Sub
    Sub Calc_NP(col, cc, c, nfr, cat)
    
    nlast = Cells(Rows.Count, col).End(xlUp).Row
    
    For i = 2 To nlast
       If cat = "*" Then
            Var1 = Cells(i, col)
            Var2 = Application.WorksheetFunction.CountIf(Range(Cells(2, c), Cells(nfr, c)), Var1)
            pct = Var2 / nfr
            Cells(i, cc) = Var2
    '        Cells(i, cc) = Var2 & "/" & Format(pct, "#0.0%")
        Else
            Var1 = Cells(i, col)
            Var2 = Application.WorksheetFunction.CountIfs(Range(Cells(2, c), Cells(nfr, c)), Var1, Range(Cells(2, 5), Cells(nfr, 5)), cat)
            pct = 0
            If Cells(i, col + 1) <> 0 Then pct = Var2 / Cells(i, col + 1)
            Cells(i, cc) = Var2 & "/" & Format(pct, "#0.0%")
        End If
        
    Next i
    
    End Sub
    Data is output to Sheet "Filtered"

    On Sheet "Filtered_Formula" enter filter criteria in K2:O2. The number of row entries must be the same for each filter field.

    in K2 enter the number of rows of filter data.

    In the attached change value in K2 (1 to 3) to see results.

    In Column E: I used the abbreviations ("P","L","B")
    Attached Files Attached Files
    Last edited by JohnTopley; 01-20-2017 at 01:31 AM.

  18. #18
    Forum Contributor
    Join Date
    10-05-2010
    Location
    CA
    MS-Off Ver
    Excel 365
    Posts
    539

    Re: Help With A Formula To Calculate Only Filtered Data?

    Thank you very much John! I can't begin to thank you enough for helping me. This is something I've been trying to figure out for weeks now. All of your help is greatly appreciated. You are an amazing forum member.

  19. #19
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,908

    Re: Help With A Formula To Calculate Only Filtered Data?

    Your are very welcome. I learned a few lessons doing the project so appreciated the chance to "have a go"

    Thank you for the feedback and rep.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.
    Last edited by JohnTopley; 01-20-2017 at 09:19 AM.

  20. #20
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,908

    Re: Help With A Formula To Calculate Only Filtered Data?

    Duplicate post.

+ 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. [SOLVED] formula when the data is filtered
    By kprabhupaul in forum Excel Formulas & Functions
    Replies: 16
    Last Post: 06-14-2016, 09:34 AM
  2. Replies: 5
    Last Post: 09-24-2015, 03:15 PM
  3. [SOLVED] how i can update my formula for calculate only filtered(seen) data
    By aaaaa34 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 11-24-2014, 07:54 AM
  4. Want to only use filtered data for SUM formula
    By jenben758 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 04-03-2013, 10:59 AM
  5. Replies: 4
    Last Post: 09-04-2012, 06:09 AM
  6. filter and automatically calculate only the filtered data
    By herolindl in forum Excel General
    Replies: 5
    Last Post: 07-05-2010, 08:15 PM
  7. Excel 2007 : Calculate Average on Filtered Data Only
    By Justinetme in forum Excel General
    Replies: 3
    Last Post: 09-16-2009, 06:01 PM

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