简体   繁体   中英

VBA - Countifs returning 0/Boolean

Having an issue with using Application.Countifs to return a value from a worksheet. Here's the existing method to call it:

Application.CountIfs(sortws.Range(sortws.Cells(1, 3), sortws.Cells(sortws.Rows.Count, 3).End(xlUp)), _
                                                  CStr(sortws.Cells(x2, 3)), _
                                                  sortws.Range(sortws.Cells(1, 4), sortws.Cells(sortws.Rows.Count, 4).End(xlUp)), _
                                                  CStr(sortws.Cells(x2, 4)), _
                                                  sortws.Range(sortws.Cells(1, x3), sortws.Cells(sortws.Rows.Count, x3).End(xlUp)), _
                                                  Chr(34) & "<>" & Chr(34) & "&0")

In this case the two variables are iteration loops - x2 is equivalent to a row on the spreadsheet, x3 is a column.

When I Print the value, it returns as 0. When I print out the requisite addresses for each range and a string for the last argument, I get this:

=COUNTIFS($C$1:$C$1201,$C$1201,$D$1:$D$1201,$D$1201,$E$1:$E$1201,"<>"&0

Which, as far as I can tell, is an appropriate function. If I copy this out in it's entirety and plug it into a worksheet cell, I get the expected value of 1.

I've printed this formula in the VBA editor using EVALUATE and it returns the same value of 0 as I get with the original formula. I've also tried putting this in a cell next to the data by using this:

sortws.Cells(x2,7).Formula = "=COUNTIFS($C$1:$C$1201,$C$1201,$D$1:$D$1201,$D$1201,$E$1:$E$1201,"<>"&0"

This returns a Boolean value of TRUE, without the formula actually being in the worksheet cell.

If I remove the last bit of the function (that is checking for matches that don't equal 0) then it correctly adds up the total number of matches for the first two columns. As such, I think something is off with my syntax for checking for 0, but I can't determine what it is. All values in that column are numeric (technically currency, but formatted as general since it's just a blank worksheet). I'm struggling to udnerstand why the same formula works when plugged into a cell manually, however.

EDIT: Updating the formula with corrected quotes allows the EVALUATE function to run, as well as putting it in a cell with .Formula . This puts the updated formula as: "=COUNTIFS($C$1:$C$1201,$C$1201,$D$1:$D$1201,$D$1201,$E$1:$E$1201," & chr(34) & "<>" & chr(34) & "&0)" , however the issue still remains with the original existing method returning 0, which uses the same method to return cells which don't equal 0 as the corrected quotes formula.

EDIT2: Resolved by @Jeeped, my existing VBA method was trying to replicate what would be required on a worksheet, simplifying the 0 check down to <>0 resolved it.

When supplying quotes within a quoted string, double them up.

sortws.Cells(x2, 7).Formula = "=COUNTIFS($C$1:$C$1201, $C$1201, $D$1:$D$1201, $D$1201, $E$1:$E$1201, ""<>0"")"

The full VBA Excel Application object COUNTIFS function should be written as follows.

Dim n As Long, x2 As Long, x3 As Long, sortws As Worksheet

Set sortws = Worksheets("sheet2")
x2 = 1201
x3 = 5

With sortws
    n = Application.CountIfs(.Range(.Cells(1, 3), .Cells(.Rows.Count, 3).End(xlUp)), _
                                 CStr(.Cells(x2, 3)), _
                             .Range(.Cells(1, 4), .Cells(.Rows.Count, 4).End(xlUp)), _
                                 CStr(.Cells(x2, 4)), _
                             .Range(.Cells(1, x3), .Cells(.Rows.Count, x3).End(xlUp)), _
                                 "<>0")
    Debug.Print n
End With

Reduce your formulas down to the minimum required to avoid string concatenation mistakes and remember to close off your formula strings with a right bracket.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM