首页 > 解决方案 > 使用 Sumifs 时类型不匹配(错误 13)

问题描述

我想添加 Q 列中所有单元格的值,这些单元格的日期在 Z 列中的现在和 30 分钟前之间,并且在 Y 列中具有特定名称。

dim nu as Date
dim ThirtyMinAgo as Date
dim currentBestTopThree as Integer

nu = WorksheetFunction.RoundDown(Now * 48, 0) / 48
ThirtyMinAgo = nu - 1 / 48

currentBestTopThree = _
    Application.SumIfs(Sheets("topthreecheck").Range("Y2:Y" & Rows.Count).End(xlUp).Row, employeelist(i), _
     Sheets("topthreecheck").Range("Z2:Z" & Rows.Count).End(xlUp).Row, "<" & nu, _
     Sheets("topthreecheck").Range("Z2:Z" & Rows.Count).End(xlUp).Row, ">=" & ThirtyMinAgo, _
     Sheets("topthreecheck").Range("Q2:Q" & Rows.Count).End(xlUp).Row)

employeelist(i)是一个数组,其中包含 Y 列中的所有唯一名称。

iFor循环的当前迭代,在1 and Ubound(employeelist)

Z 列包含格式为“2020-03-01 08:32:23”的日期

Y 列包含名称

Q 列包含数字

SumIfs 行给了我错误

类型不匹配(错误 13)

我试图硬编码一个日期:

currentBestTopThree = Application.SumIfs(Sheets("topthreecheck").Range("Z2:Z" & Rows.Count).End(xlUp).Row, #2/25/2020 9:30:00 AM#, _
Sheets("topthreecheck").Range("Q2:Q" & Rows.Count).End(xlUp).Row)

但它仍然显示错误。

我也试过:

If Sheets("topthreecheck").Range("Z2") = #2/25/2020 9:30:00 AM# Then
End If

那行并没有给我类型不匹配错误。

有人知道这里发生了什么吗?

就好像代码试图对日期求和。我想这就是它给出错误的原因。因为currentBestTopThree是整数。但我都试过了

SumIfs(Sum_range, Range1, Criteria1)

SumIfs(Range1, Criteria1, Sum_range)

其中之一应该是正确的!

标签: excelvbaexcel-formula

解决方案


推荐阅读