+ Reply to Thread
Results 1 to 21 of 21

Count of filtered rows

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Count of filtered rows

    Hi,

    I have run into this scenario before but wanted to post this here.

    I am enclosing a file to help show what i am talking about.

    I have a macro i am working on and i filter the worksheet on a column. I filter on this column such that i want to count all the filtered rows (if any) which do not equal "Level2".

    Then i use the following to count the number of rows of filtered rows........

    Please Login or Register  to view this content.
    Generally speaking the above code works well .......until today.

    For some reason i am getting last_row_pst_filter = 75 and i dont even have (as far as i know) 75 rows in the file.

    Any ideas? Why does it show 75?
    Attached Files Attached Files

  2. #2
    Forum Expert Moo the Dog's Avatar
    Join Date
    09-28-2012
    Location
    Wisconsin
    MS-Off Ver
    Office 365 (PC & Mac)
    Posts
    1,845

    Re: Count of filtered rows

    I'm thinking you need to add VBA code that hides BLANKS in the filter. Just a thought.

    - Moo

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Count of filtered rows

    hi,

    shouldnt what i have take care of blanks........because the filter should show everything that does not equal "Level2".....i thought this would include blanks as well.

    can you show me what you mean and i will try it.

    thanks for responding.

  4. #4
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Count of filtered rows

    Take a look here and run Sub makelastcell

    Place your cursor on B56 and run Sub makelastcell then your macro. I get 56
    HTH
    Regards, Jeff

  5. #5
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    865

    Re: Count of filtered rows

    Try the code below once
    Please Login or Register  to view this content.
    Harry.

  6. #6
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Count of filtered rows

    harry, i get a run time error 91 when i try your code

  7. #7
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    865

    Re: Count of filtered rows

    Not in the example below with me.
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Count of filtered rows

    i will take a look at it in the morning.....i did open the file and run it and it did run fine so i must be doing something wrong.........thanks. i will revisit this in the morning.....kinda tired.

  9. #9
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Count of filtered rows

    HSV, it does not work. It does work when there are visible elements that are filtered but if there are no visible elements filtered it does not work.

  10. #10
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Count of filtered rows

    Hi, welchs101,

    maybe use this sniplet:
    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  11. #11
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Count of filtered rows

    hi,

    HaHoBe, i have not checked out your code yet but i will.

    What i did was take HSV's code and put some error checking around it and it SEEMS to do what i want. Here is the code i used

    Please Login or Register  to view this content.
    This seems to work..........if there are no visible filtered elements i need to know this and this seems to work.


    I always seem to run into issues using the autofilter.........does anyone know of any good documents/articles which talks about using this via vba?

  12. #12
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Count of filtered rows

    I've never run into this problem using the autofilter, and if I did, run the Sub makelastcell macro from post #4...done.

  13. #13
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Count of filtered rows

    HSV,

    I was wondering if you could explain a little (not too much) this code
    Please Login or Register  to view this content.
    Not sure i understand the "offset" and ".specialcells(12)" or .specialcells(2)


    jeffreybrown,
    I dont know the last row/cell........so i dont understand how that code will help me. But thanks for posting.......i will look at it again but i dont think it applies .........

  14. #14
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    865

    Re: Count of filtered rows

    Offset= 1 down below de filter.
    Specialcells(12) = xlcelltypevisible
    Specialcells(2) = xlcelltypeconstants

    Maybe is this one better.
    Please Login or Register  to view this content.

  15. #15
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Count of filtered rows

    Hi welchs101

    Select all cells from row 57 down...delete them. Your code now returns 56. There must be some formatting or other stray "stuff" below row 56.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  16. #16
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Count of filtered rows

    HSV,

    Quick question about
    "Specialcells(2) = xlcelltypeconstants"

    What does this mean exactly?

  17. #17
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    865

    Re: Count of filtered rows

    @welchs101,

    Quick answer,

    Cells with a value.

  18. #18
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Count of filtered rows

    hi HSV,

    Thanks.

    My reason for using autofilter is that its a "quick" way of actually deleting data when you have a large file. My biggest issue with using autofilter is i always seem to run into a "problem" or "issue" where i have not taken into account some special "thing". The last time this happened i thought i finally got a good soln using auto filter........and then this latest "thing" happened.

    Can you think of any thing where the soln you proposed would not work? Just trying to think ahead...

    thanks again to ALL who responded.........thank you very much.

    Over the last 3yrs ihave learned so much from this site..........and i just keep learning..........

    thanks.

  19. #19
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Count of filtered rows

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

  20. #20
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Count of filtered rows

    One last word/question/comment:

    Last night i was doing some reading and i found this statement in a book that actually fixed my problem........i think.

    My original issue was that when counting the number of visible rows after a filter i got a value that did not make sense.......i would get a value of say 20 when there were essentially no rows visible except the header row. What i found out in the book was this

    When using the "specialcells" method one must be careful. The specialcells method includes data that was contained in the "current session" even if you have deleted that information. To "reset" one must save or execute an "activesheet.usedrange"...........

    Using the ActiveSheet.UsedRange solved my problem...........

    What i did not realize is that before "filtering" my data i had deleted some rows i did not want. I now did an "ActiveSheet.UsedRange" to reset prior to the use of specialcells method and the count of visible rows was correct. Without the "ACtiveSheet.UsedRange" the count of visible rows i think included the deleted rows i had done previously.

    Just thought i would pass this on.

    One more question i did have though. I NEVER had this problem before until i got a new computer about 2wks ago........do you think there was some setting in my old computer that i didnot have in my new computer? Just another thought.

  21. #21
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Count of filtered rows

    Hi welchs101

    do you think there was some setting in my old computer that i didnot have in my new computer?
    Probably not. I'd think you had
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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