Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
作为CountIfs标准的年份函数上的VBA类型不匹配_Vba_Excel - Fatal编程技术网

作为CountIfs标准的年份函数上的VBA类型不匹配

作为CountIfs标准的年份函数上的VBA类型不匹配,vba,excel,Vba,Excel,我从以下代码中得到一个类型不匹配错误: blattZFq3.Cells(month, siaw) = Application.WorksheetFunction.CountIfs(Worksheets(i).Range("AF10:AF290"), month, Year(Worksheets(i).Range("AE10:AE290")), minYear) 我猜这是第二个标准的问题,更具体地说,Year函数作为一个范围的标准,因为在上一个版本中,代码仅使用第一个标准并使用countif运行

我从以下代码中得到一个类型不匹配错误:

blattZFq3.Cells(month, siaw) = Application.WorksheetFunction.CountIfs(Worksheets(i).Range("AF10:AF290"), month, Year(Worksheets(i).Range("AE10:AE290")), minYear)
我猜这是第二个标准的问题,更具体地说,
Year
函数作为一个范围的标准,因为在上一个版本中,代码仅使用第一个标准并使用
countif
运行良好

minYear
被声明为Variant,并已由上一个函数指定2012的值

基本上,我希望范围
blattZFq3
中的单元格包含一个匹配
month
的数字在列中出现的次数,但前提是同一行不同列中的日期年份匹配
minYear

有人有什么建议吗


提前感谢….

与日期一起工作有时很棘手。你用的是英文版吗? 您可以尝试在Excel中编写相同的公式代码,并在将其放入VBA之前对其进行测试。您也可以尝试以下方法:

blattZFq3.Cells(month, siaw) = "=CONTIFS(.....)"

无法对数组执行此函数:
Year(工作表(i).Range(“AE10:AE290”))
,因为它希望第二个区域检查一个范围。
另外,我会避免使用单词
Month
作为变量名,因为它也是函数名

您必须使用3个条件编写函数才能绕过限制,或者在目标区域中编写公式

功能有3个标准:

blattZFq3.Cells(MyMonth, siaw) = _
    WorksheetFunction.CountIfs(Worksheets(i).Range("AF10:AF290"), MyMonth, _
    Worksheets(i).Range("AE10:AE290"), ">=" & DateSerial(minYear, 1, 1), _
    Worksheets(i).Range("AE10:AE290"), "<=" & DateSerial(minYear, 12, 31))

同时,我的解决方法有点不同……我在sub的开头添加了一个函数,该函数遍历带有日期的列(总共4列),并将年份号写入到所用部分右侧的4列中。然后我就可以引用那些专栏,就像我在这个月做的那样。谢谢你关于变量名的提示,我会记住的。。。
blattZFq3.Cells(MyMonth, siaw).Formula = _
    "=SUMPRODUCT(--(SheetName!AF10:AF290=" & MyMonth & ")," & _
    "--(YEAR(SheetName!AE10:AE290)=" & minYear & "))"