+ Reply to Thread
Results 1 to 26 of 26

Getting distinct value with multiple criteria from a data set - Using VBA countifs

  1. #1
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Dear Forum Experts,

    Need your support on the following requirements. Looking for VBA code support. Model data excel sheet has been attached for your kind reference.

    I have a scoring data sheet by state, month and employee wise. An employee can be given scored multiple times in a month also. I need an output with Distinct list of an employee only. I do not want to sum the score.

    For example: "ABC" an employee given score two times in a January month. I need an output as

    State Name Jan-20 Feb-20

    TN ABC 1 0

    Regards
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Try
    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Dear Mr. Jindon,

    Thank you so much your guidance. It works perfectly. There is only one guidance required further. I have already fixed a worksheet ("Output") for summary table. Instead of insert a new sheet with summary table. How could we get the summary table in the existing assigned sheet.

    Your guidance will be a great learning for me.

    Regards

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    Last edited by jindon; 01-17-2021 at 03:13 AM.

  5. #5
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Dear Mr. Jindon,

    Thanks a lot for your guidance. Second code also working fine. But the code is not taking the number of unique employees. Sorry, if I wrongly communicated my requirement.

    For example: There is two employees in TN state. Such as: "ABC" and "CBA", ABC was given score two times and CBA was given score one time in a January month. I need an output as. I need to take the unique value based on Column "C" only. Column C has unique Identification number for each employee.

    State Jan-20

    TN 2


    Regards

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Confising,

    If you filter by "TN", there are 4 records,
    All are the same up to col.E.
    Jan x 2, Feb x 1 in co.F

    How do you want it?

  7. #7
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Dear Mr. Jindon,

    Yes, you are right. In the existing sheet, it is 4 records. When I was testing with your code, I have newly added one employee for TN state and checked. But the code is not bringing the 2nd employee data. Attached excel sheet for your kind reference.

    Extremely thanks for your patience and sorry to bother you lot.

    Regards
    Attached Files Attached Files

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Is the that the result that you want in "result" sheet?
    If so, I can not find any difference from my code besides the cell format in heading.
    If not, upload your EXACT result that you want.

  9. #9
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Dear Mr. Jinton,

    As suggested, i have attached excel with expected result in Output_in_Pivot worksheet.

    Thanks
    Attached Files Attached Files

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    If you have PT and got what you want, why do you need vba?

  11. #11
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Dear Mr. Jinton,

    Actually This document is very large document as well as i am working for my team mates. Among them most of the people not familiar with excel and pivot. Hence, i am trying to use VBA to get the required result in a single click.

    Your support, may fulfill the requirement.

    Regards

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Try
    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Dear Mr. Jinton,

    Thank you so much for your kind guidance. It is really a great learning for me. Thanks again for your patience and support.

    Regards

  14. #14
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Dear Mr. Jinton,

    The code is perfectly working on test module, which i have shared with you, where as when i apply into real dataset it shows the runtime error 9 - Subscript out of range. I searched in google and tried few attempts. I have checked the reference worksheet name ("Master_ANC") and is there any entire column is blank in between the range proposed. Attached screenshot for your kind reference.

    Column - 266 - Month mentioned column
    Column - 257 - State mentioned column
    Column - 9 - Unique ID mentioned column.

    Could you please teach me, where exactly the error is?

    Attachment 713697

    Regards

  15. #15
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    It says "Invalid Attachment".

    Anyway, picture doesn't help at all.

  16. #16
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Dear Mr. Jinton,

    I have attached a file as binary format due to reduce the size of the file. the data set has column from A: JB. Due to confidentiality, i have removed the data from non referenced cells and column.

    The code is perfectly working on test module, which I have shared with you, where as when I apply into real dataset it shows the runtime error 9 - Subscript out of range. I searched in google and tried few attempts. I have checked the reference worksheet name ("Master_ANC") and is there any entire column is blank in between the range proposed. Attached screenshot for your kind reference.

    Column - 266 - Month mentioned column
    Column - 257 - State mentioned column
    Column - 9 - Unique ID mentioned column.

    The requirement of the summary table is,

    From column JG to KE, If any cells shows "1". I need to have a summary of those cells as said earlier. For example.

    If Column JG has "1"... then

    State - Jan - Feb - Mar

    TamilNadu 28 - 24 - 26


    Could you please teach me, where exactly the error is?
    Attached Files Attached Files

  17. #17
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    This far different from your original format.
    Are you sure that you have such blanks in col.266?

    Don't even try to think of "modify" the given code without understanding it.

  18. #18
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Yes Mr. Jindon,

    I am very new to Excel VBA, learning gradually, Basically from health sector and non technology person and passion to learn technology.
    I am really sorry, bothering you lot.

    Those cells who has "Date" only identified and registered during the month and Blanks cells are identified and registered either before or after the selected months (Column 266). We need the unique count by state wise for only mentioned months in Column 257.

    The concept is, code has to find the name of the month from column 266, (even those has blank cells) then refer Column 268 (JG) which has "1", then refer the respective state and get the unique count from column 9 (I).
    Last edited by bala04msw; 01-17-2021 at 09:40 AM.

  19. #19
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Dear Mr. Jindon,

    Now I have used a macro to extract only value available row and paste it in another sheet and apply your code and it works perfectly. I have thoroughly gone through your code and try to understand as much as possible with google help.

    Could you please me to add one more criteria in the code?. I want to get the unique value based on Column J has "1".

    Regards





    Quote Originally Posted by jindon View Post
    This far different from your original format.
    Are you sure that you have such blanks in col.266?

    Don't even try to think of "modify" the given code without understanding it.
    Attached Files Attached Files

  20. #20
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Which code are you talking about?
    If it is "Sub test_1()", it just crushes... and I don't think you understand it.

    Is this the last change you make?

  21. #21
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Dear Mr. Jindon,

    I perfectly understand your code. There is one more module (Module-2) is there, which is your original module. "Sub test_1" module is testing module, in which i was trying add something and i failed. In order to understand your code, i have gone through VBA dictionary videos and understand the flow.

    Just for the clear requirements, request you to find the attached sheet. It will make you very clear. I have done some manual output for clear understanding. Please do the needful.

    Regards
    Attached Files Attached Files

  22. #22
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    If you understand it perfectly, you could do it for yourself, so should have problem.

    What you understand is, I guess, only the column references... it doesn't mean "understand".

    VBA is not like Formula in a cell...

    You didn't answer my question.
    Quote Originally Posted by jindon
    Is this the last change you make?

  23. #23
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Dear Mr. Jindon,

    Yes, I am gradually trying to understand the VBA scripts and coming to your question. yes, this is the last change i did it.

    Regards

  24. #24
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Not really sure about "sumif".
    Please Login or Register  to view this content.

  25. #25
    Forum Contributor
    Join Date
    08-19-2016
    Location
    Chennai, India
    MS-Off Ver
    2007
    Posts
    300

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Dear Mr. Jindon,

    Thanks for your patience and support. Both codes are working and still the newly proposed criteria (Column J - only "1" value cells to be selected for count value) is not met in the first table (Summary_Tab_1) , but it works on the second "sumif" table.

    As per the current code, first table shows overall unique count value, but not filtered Column J - "1" value and "sumif" code works, where all the months have a data only. If there is no data in between months, again data start from the respective month data.

    Regards


    Regards

  26. #26
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Getting distinct value with multiple criteria from a data set - Using VBA countifs

    Not clear about what you want.
    This ends with the same figures for both result sheets.
    Only the difference is each individual vs cumulative figures.
    Change to
    Please Login or Register  to view this content.
    Last edited by jindon; 01-20-2021 at 12:55 AM.

+ 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] Countifs -Distinct with multiple criteria
    By pdalal in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 01-27-2020, 04:47 PM
  2. Countifs - Is there an easier way to count up data for multiple criteria?
    By Smurlos in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 10-30-2019, 07:16 AM
  3. Distinct function/ Formula or Unique Data based on multiple criteria
    By naveeddil in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-14-2019, 10:49 PM
  4. Countifs with multiple criteria and data range
    By Pritirus in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-21-2016, 01:18 PM
  5. [SOLVED] Count Distinct Numbers - Multiple Criteria
    By Gos-C in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-29-2013, 12:21 PM
  6. Replies: 0
    Last Post: 07-27-2011, 01:00 AM
  7. Excel 2007 : Count distinct using multiple criteria
    By ExcelinginSeattle in forum Excel General
    Replies: 2
    Last Post: 12-17-2009, 02:26 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