+ Reply to Thread
Results 1 to 8 of 8

CountIfs VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-09-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2016
    Posts
    543

    CountIfs VBA

    I am having trouble converting a SUMPRODUCT formula into a COUNTIFS. The line of code below contains the correct ranges.

     Const myEpilepsy As String = "=IF(SUMPRODUCT(--(Panel!$B$2:$B$6713=$Q$5),--(Panel!$C$2:$C$6713<=$R$5),--(Panel!$D$2:$D$6713>$R$5)),VLOOKUP($R$5,Panel!$C$2:$E$6713,3,1),""No"")"
    Here is the original COUNTIFS formula:

     =IF(COUNTIFS( A$2:A$6713,F5,B$2:B$6713,"<="&G5,C$2:C$6713, ">="&G5),VLOOKUP(G5,$B$2:$D$6713,3,1),"No")
    My attempt errors on the underlined line. (object defined or application defined error)
    VB
     Sub Calculations()
    Dim l As Long
    Dim Res As Variant
    
        Res = Application.VLookup(Range("A2").Value, Range("CA:CF"), 6, 0)
    
        Const myEpilepsy As String = "=IF(COUNTIFS(--(Panel!$B$2:$B$6713=$Q$5),--(Panel!$C$2:$C$6713<=$R$5),--(Panel!$D$2:$D$6713>$R$5)),VLOOKUP($R$5,Panel!$C$2:$E$6713,3,1),""No"")"
        Const myMarfan As String = "=IF(COUNTIFS(--(Panel!$B$25610:$B$29333=$Q$5),--(Panel!$C$25610:$C$29333<=$R$5),--(Panel!$D$25610:$D29333>$R$5)),VLOOKUP($R$5,Panel!$C$25610:$E$29333,3,1),""No"")"
    
        If Not IsError(Res) Then
             l = Sheets("annovar").Range("A" & Rows.Count).End(xlUp).Row
             
            Select Case Res
                Case "Comprehensive Epilepsy"
                    Sheets("annovar").Range("AQ5:AQ" & l).Formula = myEpilepsy
                     
                Case "Marfan Disorder"
                   Sheets("annovar").Range("AQ5:AQ" & l).Formula = myMarfan
            End Select
    
    End If
    End Sub
    Thank you.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: CountIfs VBA

    Why can't you use the original COUNTIFS formula?
    Const myEpilepsy  = "=IF(COUNTIFS( A$2:A$6713,F5,B$2:B$6713,""<=""&G5,C$2:C$6713, "">=""&G5),VLOOKUP(G5,$B$2:$D$6713,3,1),""No"")"
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    05-09-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2016
    Posts
    543

    Re: CountIfs VBA

    All the values are no longer on one sheet.

    VB
     Const myEpilepsy  = "=IF(COUNTIFS( Panel!B2:B6713,Q5,C2:C6713,""<=""&R5,Panel!D2:D6713, "">=""&R5),VLOOKUP(R5,Panel!C2:$Panel!6713,3,1),""No"")"
    ? Thanks.

  4. #4
    Forum Contributor
    Join Date
    05-09-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2016
    Posts
    543

    Re: CountIfs VBA

    When I use that code instead, I get an objecy or application defined error on the underlined line. Thanks.

    VB
     Sub Calculations()
    Dim l As Long
    Dim Res As Variant
    
        Res = Application.VLookup(Range("A2").Value, Range("CA:CF"), 6, 0)
    
        
        Const myEpilepsy  = "=IF(COUNTIFS( Panel!B2:B6713,Q5,C2:C6713,""<=""&R5,Panel!D2:D6713, "">=""&R5),VLOOKUP(R5,Panel!C2:$Panel!6713,3,1),""No"")"
    
        Const myMarfan As String = "=IF(SUMPRODUCT(--(Panel!$B$25610:$B$29333=$Q$5),--(Panel!$C$25610:$C$29333<=$R$5),--(Panel!$D$25610:$D29333>$R$5)),VLOOKUP($R$5,Panel!$C$25610:$E$29333,3,1),""No"")"
    
        If Not IsError(Res) Then
             l = Sheets("annovar").Range("A" & Rows.Count).End(xlUp).Row
             
            Select Case Res
                Case "Comprehensive Epilepsy"
                    Sheets("annovar").Range("AQ5:AQ" & l).Formula = myEpilepsy
                     
                Case "Marfan Disorder"
                   Sheets("annovar").Range("AQ5:AQ" & l).Formula = myMarfan
            End Select
    
    End If
    End Sub

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: CountIfs VBA

    I meant to say that you should change any sheet/range references if required.

    Anyway, this will not cause an error.
        Const myEpilepsy = "=IF(COUNTIFS( Panel!B2:B6713,Q5,C2:C6713,""<=""&R5,Panel!D2:D6713, "">=""&R5),VLOOKUP(R5,Panel!C2:C6713,3,1),""No"")"

  6. #6
    Forum Contributor
    Join Date
    05-09-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2016
    Posts
    543

    Re: CountIfs VBA

    It is not the formula line that throws an error it is this line: Thanks.

     Sheets("annovar").Range("AQ5:AQ" & l).Formula = myEpilepsy

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: CountIfs VBA

    The reason forthe error on that line of code is the formula being incorrect.
    Last edited by Norie; 06-28-2014 at 02:47 PM.

  8. #8
    Forum Contributor
    Join Date
    05-09-2013
    Location
    Chicago
    MS-Off Ver
    Excel 2016
    Posts
    543

    Re: CountIfs VBA

    Thank you Norie , it is working now. I has an extra space and a $ in the formula.

+ 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. COUNTIFS OR COUNTIFS needs to work
    By joshbellfi in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-23-2013, 10:55 AM
  2. COUNTIFS OR...multiple countifs without duplication in the numbers
    By HooligaD in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-21-2012, 09:53 AM
  3. Countifs Help
    By Jose9Reyes in forum Excel General
    Replies: 6
    Last Post: 02-16-2012, 08:21 AM
  4. Replies: 0
    Last Post: 12-16-2011, 09:01 AM
  5. Countifs
    By Wskip49 in forum Excel General
    Replies: 3
    Last Post: 09-27-2010, 04:27 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