简体   繁体   English

Excel VBA 使用运算符 <,>, >=, <= 和双打 function ZDBC11CAA4BD5BDA9E2D786

[英]Excel VBA use operators <,>, >=, <= with doubles as function arguments

I posted a question a few minutes ago about this matter and decided to create a new one with the problem a little better described.几分钟前我发布了一个关于这个问题的问题,并决定创建一个新的问题,更好地描述这个问题。 So I prepared a simple worksheet:所以我准备了一个简单的工作表:

工作表示例

On G4 (green) I placed the formula =SUMIFS(B1:B20;A1:A20;">="&E4;A1:A20;"<"&E5) and it works fine.G4 (绿色)上,我放置了公式=SUMIFS(B1:B20;A1:A20;">="&E4;A1:A20;"<"&E5)并且效果很好。

The cell E4 (blue) is filled by the script:单元格E4 (蓝色)由脚本填充:

Sub Button1_Click()

    Dim SH As Worksheet: Set SH = ThisWorkbook.Sheets("Sheet1")
    Dim R1 As Range: Set R1 = SH.Range(SH.Cells(1, 1), SH.Cells(20, 1))
    Dim R2 As Range: Set R2 = SH.Range(SH.Cells(1, 2), SH.Cells(20, 2))

    Dim V1 As Double: V1 = SH.Cells(4, 5).Value
    Dim V2 As Double: V2 = SH.Cells(5, 5).Value

    SH.Cells(5, 7).FormulaR1C1 = WorksheetFunction.SumIfs(R2, R1, ">=" & V1, R1, "<" & V2)

End Sub

It's easy to see that the value should be also 18 .很容易看出该值也应该是18 However it evaluates to 0 .但是它的评估结果为0

Another thing, when I declare V1 and V2 as longs:另一件事,当我将V1V2声明为 long 时:

    Dim V1 As Long: V1 = Int(SH.Cells(4, 5).Value)
    Dim V2 As Long: V2 = Int(SH.Cells(5, 5).Value)

the cell E4 (blue) evaluates to 17 (which is correct because there's 17 values between 44004 and 44005 ).单元格E4 (蓝色)的计算结果为17 (这是正确的,因为4400444005之间有17值)。

Does anyone know something about this?有人知道吗? It looks like a bug to me...对我来说它看起来像一个错误......

From your screenshot, your decimal dot is the , .从您的屏幕截图中,您的小数点是,

When you concatenate a string with a number, such as in ">=" & V1 , the number is converted to string using the current locale.当您将字符串与数字连接时,例如在">=" & V1中,数字将使用当前语言环境转换为字符串。 Your current locale has , as the decimal dot, so you end up with ">=44004,2" .您当前的语言环境具有,作为小数点,因此您最终得到">=44004,2"

Internally Excel stores all formulas according to the en-us locale, which, among other things, uses .在内部 Excel 根据 en-us 语言环境存储所有公式,其中使用. for the decimal dot.为小数点。 These are accessible via .Formula and .FormulaR1C1 properties.这些可以通过.Formula.FormulaR1C1属性访问。
The Excel interface shows you these formulas converted to your locale. Excel 界面显示这些公式转换为您的语言环境。 These are accessible via .FormulaLocal and .FormulaR1C1Local properties.这些可以通过.FormulaLocal.FormulaR1C1Local属性访问。

The functions under WorksheetFunction only expect the true internal en-us arguments. WorksheetFunction下的函数只需要真正的内部 en-us arguments。 When you pass ">=44004,2" as an argument, it causes the calculation to fail, because the function expected ">=44004.2" .当您将">=44004,2"作为参数传递时,它会导致计算失败,因为 function 应为">=44004.2" The zero result is an indication that a filter was malformed.结果为零表示过滤器格式错误。

So you should give it that :所以你应该给它

= WorksheetFunction.SumIfs(R2, R1, ">=" & Str$(V1), R1, "<" & Str$(V2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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