简体   繁体   English

Excel VBA函数出现#VALUE错误

[英]#VALUE error with Excel VBA Function

In my Excel spreadsheet I have two columns. 在我的Excel电子表格中,我有两列。

  • A contains strings with the values 'Yes', 'No' or 'Maybe'. A包含值为“是”,“否”或“可能”的字符串。
  • B contains strings with a year in. B包含一年的字符串。

I need a function to determine the number of occurrences of a year in column B, where the equivalent value in column A is 'Yes'. 我需要一个函数来确定B列中一年的出现次数,其中A列的等效值是“是”。

I currently have the following code: 我目前有以下代码:

Function CountIfYearAndValue(Rng As Range, YNM As String, Year As String) As Integer
    Dim count As Integer
    count = 0

    For Each c In Rng.Cells
        If (StrComp(Abs(c.Value), Year, vbTextCompare) = 0) And (StrComp(Cells(c.Row, A), YMN, vbTextCompare) = 0) Then count = count + 1
    Next

    CountIfYearAndValue = count
End Function

The idea of this code is that we iterate through every cell in the range given (a range on column B) and check if the year is equal to the Year parameter. 此代码的想法是,我们遍历给定范围内(B列上的范围)中的每个单元格,并检查year是否等于Year参数。 And if the equivalent cell on column A is equal to the YNM parameter we increment the count variable. 并且,如果列A上的等效单元格等于YNM参数,我们将增加count变量。

For some reason this code does not work when I use the following parameter: 由于某些原因,当我使用以下参数时,此代码不起作用:

=CountIfYearAndValue('Years'!B1:B7,"Yes","Year 7")

It just does the #VALUE error and refuses to display any outcome. 它只会发生#VALUE错误,并拒绝显示任何结果。

Any help would be much appreciated. 任何帮助将非常感激。

Edit: All of the values in both cells are on of an unformatted datatype ('General') and no cells are blank. 编辑:两个单元格中的所有值都处于未格式化的数据类型(“常规”)上,并且没有单元格为空。

It sounds like you are reinventing the wheel... There already is a built in function ( advantage: being much faster than a UDF ) that does exactly what you are after. 听起来好像您正在重新发明轮子……已经有一个内置功能( 优点:比UDF快得多 )可以完全满足您的要求。 It is called COUNTIFS() 它称为COUNTIFS()

All YES es for Year 7 in rows 1 to 10. 1至10行中Year 7年的所有YES

=COUNTIFS(B1:B10, "Year 7",A1:A10, "Yes")


I just had a quick look at your code and I think there are possibly a few reasons why your original code is not working as expected. 我只是快速浏览了一下您的代码,我认为您的原始代码无法按预期工作的原因可能有几个。

  • YNM is a valid column name therefore it should not be used as a variable name. YNM是有效的列名,因此不应将其用作变量名。 You should avoid naming your variables like that - give it a more meaningful name 您应该避免这样命名变量-给它起一个更有意义的名称

  • YNM != YMN as you had it in your code ( see function definition and then the misspelled version in the StrComp() function ) YNM != YMN就像在代码中一样( 请参见函数定义,然后查看StrComp()函数中的拼写错误的版本

  • Year is a valid VBA built in function, therefore once again you should avoid using it as a variable name as you're exposing yourself to a naming collision. Year是有效的VBA内置函数,因此,在将自己暴露在命名冲突中时,应再次避免将其用作变量名。

  • Add Option Explicit at the top of your module. 在模块顶部添加Option Explicit This requires you to Dim ension all you variables. 这要求您将所有变量Dim It's always recommended for many many reasons. 出于多种原因,总是建议使用它。

  • rng variable is of Range type therefore you do not need to explicitly add the .Cells property to it. rng变量是Range类型,因此您无需显式添加.Cells属性。 Even though it may help in some cases - at a bit more advanced level you may face some runtime type compatibility issues. 即使在某些情况下可能会有所帮助-在更高的级别上,您可能还会遇到一些运行时类型兼容性问题。 ( runtime may convert your rng Range variable to a 2D array etc ) 运行时可能会将您的rng Range变量转换为2D数组等

  • Added an explicit conversion in the second StrComp() function around the c.Offset(0, -1) as you don't want the runtime to ( rare but still possible ) convert your Yes to a Boolean data type. 在第二个StrComp()函数中,在c.Offset(0, -1)周围添加了显式转换,因为您不希望运行时( 很少但仍然可能 )将Yes转换为Boolean数据类型。 Explicit conversion to a String just gives you that extra protection ;p (lol) 显式转换为String只会为您提供额外的保护 ; p(lol)

therefore, something like this returns the correct value 因此,类似这样的返回正确的值

Function CountIfYearAndValue(rng As Range, choice As String, myYear As String) As Long
    Dim count As Long
    count = 0

    Dim c As Range
    For Each c In rng
        If (StrComp(c, myYear, vbTextCompare) = 0) And (StrComp(CStr(c.Offset(0, -1)), choice, vbTextCompare) = 0) Then
            count = count + 1
        End If
    Next c

    CountIfYearAndValue = count
End Function

Right, I hope this helps you understand bits and pieces :) any questions please leave a comment 是的,我希望这可以帮助您理解点点滴滴:)如有任何问题,请发表评论

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

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