简体   繁体   中英

VBA countifs using multiple Arrays

I an looking to find a value from countifs ,using an array of dates and an array of names.

""Shout out to Jeeped for providing the code for date array below.""

Dim d As Long, dts As Variant
ReDim dts(DateSerial(2019, 1, 1) - DateSerial(2018, 1, 1) + 1)
    dts(0) = vbNullString
For d = DateSerial(2018, 1, 1) To DateSerial(2018, 12, 31)
    dts(d - DateSerial(2018, 1, 1) + 1) = Format(d, "m/d/yyyy") '<~~should match worksheet
Next d

R = Application.Sum(Application.CountIfs(Range("A:A"), "Stuff", Range("B:B"), dts, Range("C:C"), Array("Name1", "Name2")))

The result is always zero, and "I think?" its because both of the arrays are of different types? or maybe format is a better word.

I can use the dts array in similar countifs ...see below.

Z = Application.Sum(Application.CountIfs(Range("A:A"), "stuff", Range("B:B"), dts))

however as soon as I need to introduce another array for say "names", the countifs line will execute and the code runs, but.. just returns zero.

R = Application.Sum(Application.CountIfs(Range("A:A"), "Stuff", Range("B:B"), dts, Range("C:C"), Array("Name1", "Name2")))

How can I create an array of "names" that would work with the dts array in a countifs situation?

Thanks!

Edit 4/2/2018 1:00pm Adding Example to show that changing the order still returns 0.

within worksheet use this, or re-order for the other line commented out in the code below. enter image description here Then using either the active line of the one commented out.

Public Sub Broken()
'****************************************************************
Dim d As Long, dts As Variant
ReDim dts(DateSerial(2019, 1, 1) - DateSerial(2018, 1, 1) + 1)
dts(0) = vbNullString
For d = DateSerial(2018, 1, 1) To DateSerial(2018, 12, 31)
dts(d - DateSerial(2018, 1, 1) + 1) = Format(d, "m/d/yyyy") '<~~should match 
worksheet
Next d
'****************************************************************

        'Answer = Application.Sum(Application.CountIfs(Range("A:A"), "Z*", Range("B:B"), dts, Range("C:C"), Array("Name1", "Name2", "Name3", "Name4", "Name5")))

        Answer = Application.Sum(Application.CountIfs(Range("A:A"), dts, Range("B:B"), "Z*", Range("C:C"), Array("Name1", "Name2", "Name3", "Name4", "Name5")))

        MsgBox Answer, vbInformation
End Sub

****The answer should be 2

*Date To be within 2018 Or Blank from dts Array *a Column to contain "Z" *a Column to contain a name from name Array

Documentation for the WorksheetFunction commands isn't great. It's a good idea to refer to the documentation for the related worksheet function. (ie., COUNTIFS vs Application.WorksheetFunction.Countifs )

Looks like your added criteria are out of order.


COUNTIFS Syntax

COUNTIFS( criteria_range1, criteria1 
        [,criteria_range2, criteria2
        [,criteria_range3, criteria3
        [,criteria_range4, criteria4]]] … )

The COUNTIFS function syntax has the following arguments:

  • criteria1 (Required) The first range in which to evaluate the associated criteria.

  • criteria1 (Required) The criteria in the form of a number, expression, cell reference, or text that define which cells will be counted.
    For example, criteria can be expressed as 32 , ">32" , B4 , "apples" , or "32" .

  • [criteria_range2, criteria2,] ... (Optional) Additional ranges and their associated criteria.
    Up to 127 range/criteria pairs are allowed.

Important:

  • Each additional range must have the same number of rows and columns as the criteria_range1 argument. The ranges do not have to be adjacent to each other.

More Information:

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