VBA SumIfs 不适用于日期范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15788592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:22:39  来源:igfitidea点击:

VBA SumIfs not working with Date range

excelvbaexcel-vba

提问by ftkg

So, I have tried many variations, but I can't make this work:

所以,我尝试了很多变体,但我无法完成这项工作:

var_sum = WorksheetFunction.SumIfs(Range("H:H"), Range("B:B"), str_client, Range("A:A"), "<=" & date_var)

This formula has 2 conditions. The second one (date comparison) breaks it and makes the sum = 0. date_var is a Date variable. The date range in Excel is formatted as Date.

这个公式有2个条件。第二个(日期比较)打破它并使总和 = 0。 date_var 是一个日期变量。Excel 中的日期范围被格式化为日期。

What could be wrong?

可能有什么问题?

回答by SeanC

I have a feeling you have a string with the date.

我有一种感觉,你有一个带日期的字符串。

Date_Var = "01/01/2013"

you could try using CDate(or CLng(to convert it to the appropriate value, as Excel stores dates as number of days past Jan 1, 1900

您可以尝试使用CDate(CLng(将其转换为适当的值,因为 Excel 将日期存储为 1900 年 1 月 1 日之后的天数

var_sum = WorksheetFunction.SumIfs(Range("H:H"), Range("B:B"), str_client, _
    Range("A:A"), "<=" & CLng(date_var))