简体   繁体   中英

#VALUE error with Excel VBA Function

In my Excel spreadsheet I have two columns.

  • A contains strings with the values 'Yes', 'No' or 'Maybe'.
  • B contains strings with a year in.

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'.

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. And if the equivalent cell on column A is equal to the YNM parameter we increment the count variable.

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.

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. It is called COUNTIFS()

All YES es for Year 7 in rows 1 to 10.

=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. 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 )

  • 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.

  • Add Option Explicit at the top of your module. This requires you to Dim ension all you variables. 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. 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 )

  • 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. Explicit conversion to a String just gives you that extra protection ;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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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