简体   繁体   English

Excel UDF IF语句<>字符串引发#Value错误

[英]Excel UDF IF Statement <> String Raises #Value Error

Hello I am working on a larger Excel UDF apart of it is comparing two different strings to see if they are not equal. 您好,我正在研究一个更大的Excel UDF,它正在比较两个不同的字符串以查看它们是否不相等。 When I check to see if the cell in my range is not equal to my desired string ("red" in this example) then add a value which is one row to the right to the counter. 当我检查范围内的单元格是否不等于所需的字符串(在此示例中为“红色”)时,请在计数器右侧添加一行值。 When I do this it raises a #value error. 当我这样做时,会引发#value错误。

It is interesting though that if I do the same thing but only add values that are equal to "Red" it doesn't raise an error. 有趣的是,如果我做同样的事情,但只添加等于“ Red”的值,则不会引发错误。

Function SUM_IF_NOT_RED(Creiteria_Rnage As Range, Sum_range As Integer) as Double

Dim counter As Double
Dim Cell As Range

For Each Cell In Creiteria_Rnage .Cells

If (Cell.Value <> "Red") Then
    counter = counter + Cell.Offset(0, Sum_range).Value
End If

Next Cell

SUM_IF_NOT_RED = counter
End Function

I know I can do sum(range) - sumif("Red",range) to get the answer but I'm curious why this UDF raises an error when you set it to <> but works fine when its set to =. 我知道我可以做sum(range)-sumif(“ Red”,range)来获得答案,但是我很好奇为什么将此UDF设置为<>时会引发错误,但将其设置为=时却可以正常工作。

The function input parameter are variant. 函数输入参数是变量。 I have also included a parameter col which should be integer and its the difference between the criteria column and the column from where we want to pick the data. 我还包含了一个参数col,它应该是整数,并且它是criteria列和我们要从中选择数据的列之间的差。

Function SUM_IF_NOT_RED(Creiteria_Range, col) As Double

    Dim counter As Double
    Dim Cell As Range

    For Each Cell In Creiteria_Range.Cells

        If (Cell.Value <> "Red") Then
            counter = counter + Cell.Offset(0, col)
        End If

    Next Cell

    SUM_IF_NOT_RED = counter
End Function

在此处输入图片说明

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

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