+ Reply to Thread
Results 1 to 38 of 38

Using VBA to filter multiple criteria and then count number of rows with data

  1. #1
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Using VBA to filter multiple criteria and then count number of rows with data

    Hello,

    The goal is to filter some date for a set of criteria and then count the number of rows of data. All the filtering works.
    Only the last line (t = application.worksheetfunction......) gives me an error when I complie. It actually highlights the semi-colon in the range (A2:A65000) when I step through the code by pressing F8.

    I don't know why.



    Please Login or Register  to view this content.
    Last edited by ajohns52; 09-25-2018 at 07:06 AM. Reason: selecting code

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    You need to use Range like
    Please Login or Register  to view this content.
    Also please amend you post to put the code in code tags, the # icon in the reply window

  3. #3
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    thanks!! I had another quick question.

    I would like to take the value of t and put it in another workbook (called Inventory_Report) in cell K2. This was my thought process. Simply set the variable t and equal it to another cell value in the Inventory_Report workbook.



    Please Login or Register  to view this content.
    I just saw your message about putting the code in tags. Did I do this correctly now? I'm still not quite sure how the tags are placed.
    Last edited by ajohns52; 09-25-2018 at 07:06 AM. Reason: code in tags

  4. #4
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    You need to edit both your posts, select the code & then click the # icon in the reply/edit window.
    If done correctly it should then appear in a "window" similar to line of code in post#2

  5. #5
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    hello Fluff13, i have correctly tagged the code.

    Regards

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    Hi, to put the value of t into another workbook, that line should be
    Please Login or Register  to view this content.
    not the other way round.

  7. #7
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    Thanks for the help so far! I feel like I'm almost there. I get an error in the last line of code. Do I have first to activate the sheet "Leitfaden"




    Please Login or Register  to view this content.

  8. #8
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    You're missing the quotes around the sheet name

  9. #9
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    Hello, thanks that worked.

    I have just about the same piece of code as before , only the last line is different which is where the value t is being pasted to. However I get an error stating the "index is out of range".

    Please Login or Register  to view this content.

  10. #10
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    That's not an error I remember seeing, what line is highlighted when you hit debug?

  11. #11
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    I have underlined the line of code

    Please Login or Register  to view this content.

  12. #12
    Valued Forum Contributor
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,257
    Quote Originally Posted by ajohns52 View Post
    I have underlined the line of code

    Please Login or Register  to view this content.
    Maybe like this

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    hello, yeah it still doesn't work. Weird thing is as soon as i switch the last line of code by only pointing to another worksheet, it all works.

    The following works in the above code:

    Please Login or Register  to view this content.

  14. #14
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    In that case it sounds like you have the sheet name spelt wrongly.
    Should the underscores be there?
    Also check for leading/trailing spaces

  15. #15
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    yeah it doesn't work for some reason. I even changed the worksheet name to just "Report" and it still doesn't work. I'll look more into it and let you know if i find a solution

  16. #16
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    and the official error which i didn't state correctly is:

    Run-time Error 9 "Subscript Out of Range"

  17. #17
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    With the "Report_Inventory_Stock" sheet active run this
    Please Login or Register  to view this content.
    Then copy what was written in the Immediate window in the VBE, to the thread.

  18. #18
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    i don't know if this is what you're looking for. There are several Windows open in the console

    Please Login or Register  to view this content.

  19. #19
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    That's exactly what I was looking for, thanks.
    You have a space at the end of the sheet name, which is why you are getting the error.

  20. #20
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    that's it! great tool to use. I will keep this in my box of tools!

    Thanks

  21. #21
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    Glad to help & thanks for the feedback.

    I would also recommend using the file extension when referring to workbooks
    Please Login or Register  to view this content.
    with out the extension, if somebody with different folder settings tries to use your code it may still fail.

  22. #22
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    okay thanks for the recommendation. Another quick statement. The ultimate goal of what I'm trying to do is copy and paste these values to the worksheet "inventory_stock_report" for each month. In the inventory_report_stock worksheet i have the months January, February, March etc. Now imagine for the above code, the copy and paste is hard-coded (i.e. put the value t in cell Q2): However for next month, the cell will probably be R2. That means I would have to go in the code and change the code from Q2 to R2.

    Do you know of any resources where I could learn about the dynamic changing of cells. I would ideally like to push a button and then populate the list for the month that I'm working in. And then when November comes around I would do the same thing.

    Regards

  23. #23
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    If the column headers are text, rather than dates, you could use something like
    Please Login or Register  to view this content.

  24. #24
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    Hello I took some of your advice from the above code,

    I'm trying to find the letter A in the range B11:B14, and if I find it I would like to go to column 6 (staying in the same row) and grab that value. So basically if there was an "A" in B12, I would like to grab the value in F12 and store it to the variable partA_quantacc1. I'm receiving an error in the "If Application.Match" piece of code. I underlined it.

    Please Login or Register  to view this content.

  25. #25
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    If you get an error on that line I suspect it's because it find "A" in the range

  26. #26
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    exactly.... I want it to find an A in the range, so why wouldn't it continue with the rest of the code in the IF Statement?

  27. #27
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    and there is no "A" in the range B11:B14 currently, so it should follow through to the else Statement...correct?

  28. #28
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    Nope, the Match will generate an error if nothing is found.
    Do it like
    Please Login or Register  to view this content.

  29. #29
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    Hello I think I may have found another solution. Let me explain what I'm trying to do. Let's say my Excel table looks like the one below. I would like to get the average of all the values for Department A. The 1st column is Department and the 2nd column is Quantity. For example, the average for Department A would be (4 + 6 + 10) / 3.

    Therefore, I need some type of loop. I wrote this small piece of code:
    (I just added the average function while in this post so i don't know if it's completely right)
    The Department column in my Excel file is column 2 and the Quantity value is column 6.
    I received some Errors. Most of it was marked red.

    Please Login or Register  to view this content.



    A 4
    B 7
    C 3
    D 5
    ________________________________
    A 6
    B 1
    C 9
    D 5
    _______________________________
    A 10
    B 5
    C 3
    D 4



    I used some of your code that you wrote in the previous post, but I only get the value for the 1st Department A and not the other Department A values so i decided to write the above code.

    Please Login or Register  to view this content.
    Last edited by ajohns52; 10-11-2018 at 04:20 AM.

  30. #30
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    You don't need a loop, you can just use AverageIf
    Please Login or Register  to view this content.

  31. #31
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    haha thanks! It works! just one line of code.

    However just out of curiousity, I would like to know how I would store it in an array. This code below works but everytime there is a new value for Department A, the value overwrites the previous value for Department A.


    Please Login or Register  to view this content.

  32. #32
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    I think it's time to start a new thread for this, as we have gone a very long way from the original question.

  33. #33
    Registered User
    Join Date
    10-11-2018
    Location
    Gurgaon
    MS-Off Ver
    XP
    Posts
    10

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    I tried and failed to do this..

  34. #34
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    contactforinfo
    Please do not "hijack" other peoples threads.
    You will need to start a thread of your own.

  35. #35
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    you're absolutely right. I'll start a new thread. Thanks for all of your help

  36. #36
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    You're welcome

  37. #37
    Registered User
    Join Date
    09-12-2018
    Location
    Berlin
    MS-Off Ver
    2016
    Posts
    23

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    hello Fluff13,

    I am trying to start a new thread but i can't seem to find the button. Is there a maximum number of threads that I can start? It says you're a Forum Expert so I'm asking you.

  38. #38
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,070

    Re: Using VBA to filter multiple criteria and then count number of rows with data

    Quote Originally Posted by ajohns52 View Post
    Is there a maximum number of threads that I can start?
    Not that I'm aware of.

+ 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. Replies: 4
    Last Post: 11-01-2016, 03:28 PM
  2. [SOLVED] Count number of occurrences based on criteria in multiple columns and rows
    By gerryger in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-22-2015, 08:13 PM
  3. [SOLVED] Count number of rows meeting either criteria
    By abousetta in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 11-14-2014, 05:35 AM
  4. How to count the number of rows that meet multiple selection criteria
    By mcnallyb in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-21-2014, 11:26 AM
  5. Count number of Rows with multiple criteria (multiple valued cells)
    By garog in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 03-09-2014, 01:33 PM
  6. Trying to count number of rows where criteria are met in columns
    By kfloyd in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-10-2013, 01:45 PM
  7. Count number of rows with multiple criteria
    By spinx in forum Excel General
    Replies: 5
    Last Post: 09-17-2010, 09:17 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