+ Reply to Thread
Results 1 to 28 of 28

Macro to COUNTIF in a consolidated file for rows meeting certain criteria

  1. #1
    Registered User
    Join Date
    07-13-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    34

    Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    Hi. I have multiple files with a key field each.
    - The key field can be located in any column - the column is not known in advance.
    - All key fields per file are listed in a consolidated file from the left to the right in a table such as File 1, File 2 File 3, File 4, File 5, File 6. CHECK fields are comparing if the values in 2 fields to the left match. The number of files - 6 is an example. It can be larger (up to 50).
    - Hyperlink to each file is provided in the consolidated file.
    - I have to count how many lines are in the File1, File2, File3, File4, File5, File6 in the certain field (field name is provided in line 4 for each file in the Consolidated file) match Criteria ID. This could be ID1, ID2, IDs. Criteria for each file is also provided (criteria ID is provided in line 5 for each file in the Consolidated file).


    I have to loop through all the files to count number of IDs in a given field meeting given criteria and populate COUNTIF values to the fields File1, File2, File3, File4, File5, File6 in the consolidated file. This should be done with VBA instead of current formulas provided in File1, File2, File3, File4, File5, File6 fields.

    Please see consolidated file and sample files File1, File2, File3, File4, File5, File6 attached. The file should be unzipped to C:\ directory to enable hyperlinks.

    Thank you.
    Last edited by Tshulha; 05-22-2023 at 10:47 PM.

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

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    This doesn't use CountIf function, so no need to open each files.
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    07-13-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    34

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    jindon, this is awesome! It works! Thank you

  4. #4
    Registered User
    Join Date
    07-13-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    34

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    hi jindon. Thank you for the code. How should the code be edited if files are split into 2 subfolders? For ex, File 1, File 3, File 5 are located in C:\COUNT VBA\Extract, while File 2, File 4, File 6 are located in C:\COUNT VBA\Load. We can move from the consolidated file's directory to 2 subfolders: Extract and Load. Thank you

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

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    If the numbers at the end of the file name always one digit then
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    07-13-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    34
    Quote Originally Posted by jindon View Post
    If the numbers at the end of the file name always one digit then
    Please Login or Register  to view this content.
    Thank you, jindon. The name of the file could be
    be any and is indicated in line 3. Examples of the file names are BioInfo, EmploymentInfo, CompInfo, Emails, Phones, PaymentDetails, etc.

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

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    07-13-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    34

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    jindon, thank you. It looks like it does not work. Please see attached how the files are organized and named. The number of pairs of files can be bigger.
    Last edited by Tshulha; 05-22-2023 at 10:47 PM.

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

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    You have changed the data layouts.

    Is this the last change?
    If not, need to see exact layouts.

  10. #10
    Registered User
    Join Date
    07-13-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    34
    Quote Originally Posted by jindon View Post
    You have changed the data layouts.

    Is this the last change?
    If not, need to see exact layouts.
    I will send the last layout soon with the maximum possible number of columns

  11. #11
    Registered User
    Join Date
    07-13-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    34

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    Quote Originally Posted by jindon View Post
    You have changed the data layouts.

    Is this the last change?
    If not, need to see exact layouts.
    jindon, please see final layout attached.
    Last edited by Tshulha; 05-22-2023 at 10:48 PM.

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

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    Now the code finds the file location from the Link assigned by Hyperlink formula.
    If the link is invalid, that column will be left blank.

    You need to be very careful that row 4, A4:EI4 to be exact, should be kept complete blank.
    Getting correct data range is the one of the most important factors in coding.
    Please Login or Register  to view this content.
    Edit
    Code has been cleaned:
    Last edited by jindon; 05-21-2023 at 12:13 AM. Reason: Code has been cleaned.

  13. #13
    Registered User
    Join Date
    07-13-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    34

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    jindon, thank you. Now everything works great except lines 9 and 10: in line 9 formulas are overwritten by blank cells and CHECK formula, in line 10 headers are now Column1, Column2, 0, Column3, Column4, 02, Column5, Column6, 03, etc. Is it something that can be fixed by starting validation from line 11 instead of 9?

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

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    OOps, missed to change one line, 5 to 7.

    Code in #12 has been fixed.

  15. #15
    Registered User
    Join Date
    07-13-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    34

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    thank you, jindon. Now the code works for Extract and Load fields in lines 9 and 10, but the issue still persists for CHECK columns in lines 9 and 10. Would it be possible to fix this?

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

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    One more same change, see #12.

  17. #17
    Registered User
    Join Date
    07-13-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    34

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    thank you. Almost great except the code adds 2 more lines at the bottom with blank IDs and formulas generated by the code. If this is hard to fix, we can leave as it is and I will be manually deleting the lines each time the code is run, but if this is an easy fix, it would be great to know how the lines can be removed Attachment 830017
    Also, I noticed that the formula returns 1 even if there are multiple records for a certain ID. For ex, it is expected to return 3 if there are 3 lines for the selected ID in the file. Can this be fixed?
    Last edited by Tshulha; 05-21-2023 at 01:20 AM.

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

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    Can not replicate it, so upload the workbook that you have such problem.

  19. #19
    Registered User
    Join Date
    07-13-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    34

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    please see attached. The code is saved in ThisWorkbook. I copied-pasted #12 and the problem with 2 extra lines is gone.

    Only formula IF requires replacement with COUNTIF: for ex, in Extract_Phones users A1, A2, A3, A4, A5, A6, A7, A8 have 5 records each - expectation to see 5,5,5,5,5,5,5,5 in the consolidated file in the correspondent column. Also, in Load_14. Phones users C2, C3, C4, C5, C6, C7, C8, C9 have 5 records each - expectation to see 5,5,5,5,5,5,5,5 in the consolidated file in the correspondent column.

    For ex, for Load_14. Phones: the code should search for the column named "personInfo.person-id-external" in the "Load_14. Phones" file, count the number of entries for each user from column "ID3" in the Consolidated file and place this count to the field "Load_14. Phones" in the Consolidated file.
    Capture1.PNG

    I changed the formula to this one:
    .FormulaR1C1 = "=if(isnumber(match(rc" & c(0) & "," & fn & "c" & c(1) & ":c" & c(1) & ",0)),countif(" & fn & "c" & c(1) & ":c" & c(1) & "," & "rc" & c(0) & "),0)"

    This formula works, but Consolidated file returns values only when the target files are open, otherwise the cells return #VALUE! since Excel cannot calculate values from the closed files. What about opening each file in the code, populating formulas, evaluating values for the entire column, copying and pasting values over the formulas and then closing the file for each column in the range S:EH?
    Last edited by Tshulha; 05-22-2023 at 10:48 PM. Reason: added zip file, logic screenshot, possible updated formula

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

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    If we are testing the codes with different files, we never come to the end.
    All your files uploaded before the last one have no duplicates, so I used Match function.
    change
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  21. #21
    Registered User
    Join Date
    07-13-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    34

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    this one works perfect! thank you, jindon!

    Just the last question: if I have more than 1000 records, let's say, 10000, should I replace 1000 by 10000 in the code?

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

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    Correct.
    Sumproduct function with full column reference will take time, so better apply possible maximum row to limit.

  23. #23
    Registered User
    Join Date
    07-13-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    34

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    thank you, jindon. Your solution is very clean and elegant. I like it very much!
    Last edited by Tshulha; 05-21-2023 at 09:48 PM.

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

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    Glad you like it.

    Also, you may not be aware that you can thank by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation.

  25. #25
    Registered User
    Join Date
    07-13-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    34

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    sure, jindon. Done.

  26. #26
    Registered User
    Join Date
    07-13-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    34

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    hi jindon. I was wondering if it is easy to make the change in this code to skip the column if the hyperlink is not valid instead of populating NULL?

    Sub extract_vs_load()
    Dim fn As String, s As String, c(1), i As Long, ii As Long, iii As Long
    Application.ScreenUpdating = False
    With Sheets("dashboard").[a5].CurrentRegion
    For ii = 19 To .Columns.Count Step 3
    For iii = 0 To 1
    .Cells(7, ii + iii).Resize(.Rows.Count - 6).ClearContents
    If .Cells(2, ii + iii).Formula Like "=HYPERLINK(*" Then
    s = Replace(Split(Split(Mid$(.Cells(2, ii + iii).Formula, 2), ",")(0), "(")(1), """", "")
    If Dir(s) <> "" Then
    fn = "'" & Left$(s, InStrRev(s, "")) & "[" & Mid$(s, InStrRev(s, "") + 1) & "]Sheet1'!"
    c(0) = Application.Match(.Cells(4, ii + iii), .Rows(6), 0)
    c(1) = ExecuteExcel4Macro("match(""" & .Cells(3, ii + iii) & """," & fn & "r1:r1,0)")
    With .Cells(7, ii + iii).Resize(.Rows.Count - 6)
    If (IsNumeric(c(0))) * (IsNumeric(c(1))) Then

    .FormulaR1C1 = "=sumproduct((" & fn & "r2c" & c(1) & _
    ":r10000c" & c(1) & "=rc" & c(0) & ")+0)"
    End If
    End With
    End If
    End If
    Next
    .Cells(7, ii + iii).Resize(.Rows.Count - 6).FormulaR1C1 = "=rc[-2]=rc[-1]"
    Next
    End With
    Application.ScreenUpdating = True
    End Sub

    Thank you!

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

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    Just delete one line in red
    Please Login or Register  to view this content.

  28. #28
    Registered User
    Join Date
    07-13-2018
    Location
    Canada
    MS-Off Ver
    2016
    Posts
    34

    Re: Macro to COUNTIF in a consolidated file for rows meeting certain criteria

    great! it worked. thank you

+ 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: 5
    Last Post: 04-23-2020, 04:21 PM
  2. Excel formula required similar to VLookup
    By Mustafa Ahmedali in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-16-2019, 04:35 AM
  3. COUNTIF formula only after meeting another cell's not null criteria
    By kdestef1 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-06-2017, 12:19 PM
  4. [SOLVED] Count Data Meeting Criteria Across Columns Meeting Criteria
    By idelta in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-06-2015, 03:53 PM
  5. Macro to delete rows meeting criteria
    By D_N_L in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-23-2012, 05:21 AM
  6. Removal of rows meeting certain criteria
    By smurray444 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-26-2007, 07:38 AM
  7. selecting row data meeting countif criteria
    By april2579 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-14-2006, 11:25 AM

Tags for this Thread

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