+ Reply to Thread
Results 1 to 17 of 17

Convert CountIfs formula to VBA

Hybrid View

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

    Convert CountIfs formula to VBA

    I have the following excel worksheet formula:

    =COUNTIFS(Inputfile_InQQR!$B$2:$B$5123,"=" & Summary_InQQR!A2,Inputfile_InQQR!$W$2:$W$5123,"Yes",Inputfile_InQQR!$F$2:$F$5123,"Random",Inputfile_InQQR!$AA$2:$AA$5123,"Data Error",Inputfile_InQQR!$C$2:$C$5123,IF((Summary_InQQR!C2="Overall"),"<>" & """",IF((Summary_InQQR!C2)="ABC","ABC","XYZ")))

    I am trying to convert this to a vba formula........my issue is with the "IF" statement that i have inside the countifs function.............not sure how to handle this.

    I am using in vba the "Application.WorksheetFunction.CountIfs" to duplicate the formula. However, like i said i am not sure how to handle the "IF" statement inside the countifs.

    any ideas?

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Convert tricky excel wrksht formula to VBA formula.......its a countifs

    Why not just keep the formula in Excel and get the value in your code?

    Why do you want to convert it to VBA? VBA will be slower

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

    Re: Convert tricky excel wrksht formula to VBA formula.......its a countifs

    its a long story but instead of using this formula in excel and then filling down a bunch of formulas i am doiong everything in arrays and then pasting the values stored in the arrays.........

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

    Re: Convert tricky excel wrksht formula to VBA formula.......its a countifs

    This is all i could come up with...........

    its in a for loop

    If sht_array3(x, 1) = "Overall" Then
    
    
        unit_de_array(x, 1) = (Application.WorksheetFunction.CountIfs( _
            date_range, "=" & date_array1(x, 1), _
            Error_YN_Range, "=" & "Yes", _
            TypeReview_Range, "=" & "Random", _
            Errorcat_Range, "=" & "Data Error", _
            Site_Range, "<>" & ""))
            
    
    ElseIf sht_array3(x, 1) = "ABC" Then
    
        unit_de_array(x, 1) = (Application.WorksheetFunction.CountIfs( _
            date_range, "=" & date_array1(x, 1), _
            Error_YN_Range, "=" & "Yes", _
            TypeReview_Range, "=" & "Random", _
            Errorcat_Range, "=" & "Data Error", _
            Site_Range, "=" & "ABC"))
            
    
    ElseIf sht_array3(x, 1) = "XYZ" Then
    
        unit_de_array(x, 1) = (Application.WorksheetFunction.CountIfs( _
            date_range, "=" & date_array1(x, 1), _
            Error_YN_Range, "=" & "Yes", _
            TypeReview_Range, "=" & "Random", _
            Errorcat_Range, "=" & "Data Error", _
            Site_Range, "=" & "XYZ"))
            
    
    End If

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Convert tricky excel wrksht formula to VBA formula.......its a countifs

    I really think you should reconsider, whilst useful, worksheet functions in VBA are generally slow and to use this many of them, I'd need an exceptionally good reason - one I can't think of from the top of my head.

    Can you tell us a bit more about what you're trying to do and we might be able to make some alternative suggestions

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

    Re: Convert CountIfs formula to VBA

    i have this big excel file........and its taking a long time to run.......i found (through this site asking questions) that the cause of my program taking a long time to run is the calculations/formulas autocalculating...........so i am removing those formulas in my file that are not needed and in their place i am using arrays to calculate the values. I am then putting the values in the cells instead of the formulas.

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,973

    Re: Convert CountIfs formula to VBA

    Why not just put the formulas in the cells then convert them to values immediately?
    Everyone who confuses correlation and causation ends up dead.

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

    Re: Convert CountIfs formula to VBA

    so i put in the formulas......then i do a fill down.........then i would have to convert to values........

    how would you do this? i have my ideas but i thought it would take longer that way.

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,973

    Re: Convert CountIfs formula to VBA

    For example:
        With Range("A1:A1000")
            .FillDown
            .Value2 = .Value2
        End With
    Note: if you have calculation set to manual, you'll need to calculate the range first, or use the Formula property to assign the formula to the cells, and then convert to values.

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,973

    Re: Convert CountIfs formula to VBA

    PS you can also use Evaluate with your original formula string and assign the result to the range directly if you prefer.

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

    Re: Convert CountIfs formula to VBA

    is this the way you would do it instead of using the application.worksheetfunctions?

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,973

    Re: Convert CountIfs formula to VBA

    Yes, it is.
    (well, strictly speaking, I would probably redesign the entire workbook if it were that slow)

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

    Re: Convert CountIfs formula to VBA

    thats kinda what i am trying to do via not using formulas where i can but instead values........

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

    Re: Convert CountIfs formula to VBA

    so how wouldi use the Evaluate.........

  15. #15
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,973

    Re: Convert CountIfs formula to VBA

    That's not really redesigning, just working around the issue.
    Did you try the previous code?

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

    Re: Convert CountIfs formula to VBA

    yes, with the .value inside the with statement.......worked great.........just wish i had known about this earlier..................

  17. #17
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,973

    Re: Convert CountIfs formula to VBA

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

    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

+ 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