如何在VBA Excel UDF参数中使用比较运算符?

我将如何编写UDF,以便他们接受比较运算符的参数?

当使用标准函数时,您可以这样写= countif(range; x) ,您将得到等于x的范围内的单元格数量。

在VBA中复制这个函数看起来像这样:

Function countifUDF(rng As Range, x As Integer) count = 0 For Each cell in rng.Cells If cell.Value = x Then count = count + 1 Next cell countifUDF = sum End Function 

当使用标准函数时,可以将比较运算符传递给函数,如this = countif(range;“<”&x) ,您将得到范围小于x的单元格数。

我怎么能在UDF中做到这一点? 我的UDF as = countifUDF(range;“<”&x)产生#VALUE


 Function countifUDF(rng As Range, x As String) Dim arr() As String Dim count As Integer Dim i As Integer ' breaking down x character by character and puts in array ReDim arr(Len(x) - 1) For i = 1 To Len(x) arr(i - 1) = Mid$(x, i, 1) Next ' if the last character in x is not numeric i assume the user want to count matching strings ' Like allows the user to use wildcards, LCase makes the comparision case insensitive If IsNumeric(arr(UBound(arr))) = False Then x = LCase(x) For Each cell In rng.Cells If LCase(cell.Value) Like x Then count = count + 1 End If Next cell ' if the first char in x is numeric its pretty straight forward ElseIf IsNumeric(arr(0)) = True Then For Each cell In rng.Cells If cell.Value = x Then count = count + 1 End If Next cell ' if the first character in x is < and the second is numeric less-than operator is used ElseIf arr(0) = "<" And IsNumeric(arr(1)) = True Then ' removing < from x x = Replace(x, "<", "") For Each cell In rng.Cells If cell.Value < x Then count = count + 1 End If Next cell ElseIf arr(0) = ">" And IsNumeric(arr(1)) = True Then x = Replace(x, ">", "") For Each cell In rng.Cells If cell.Value > x Then count = count + 1 End If Next cell ' if the first char is < and the second is > the is not operator is used ElseIf arr(0) = "<" And arr(1) = ">" Then x = Replace(x, "<", "") x = Replace(x, ">", "") For Each cell In rng.Cells If cell.Value <> x Then count = count + 1 End If Next cell ElseIf arr(0) = ">" And arr(1) = "=" Then x = Replace(x, ">", "") x = Replace(x, "=", "") For Each cell In rng.Cells If cell.Value >= x Then count = count + 1 End If Next cell ElseIf arr(0) = "<" And arr(1) = "=" Then x = Replace(x, "<", "") x = Replace(x, "=", "") For Each cell In rng.Cells If cell.Value <= x Then count = count + 1 End If Next cell End If countifUDF = count End Function 

鉴于我得到的答案似乎在VBA中处理比较运算符在UDF中没有方便的方式,请纠正我,如果我错了。 我的解决scheme支持通配符的数字和string。 起初我试图用&as分隔符来使用分割方法。 出乎意料的是,VBA将“>”&x'标识为“> x”,为什么我必须按字符分割x个字符,并评估用户input的比较运算符types。

UDF()将第二个参数视为一个string

 Function countifUDF(rng As Range, x As Variant) As Long Dim cell As Range, Count As Long, CH As String, VL As Long VL = Replace(Replace(x, ">", ""), "<", "") CH = Left(CStr(x), 1) Count = 0 If CH = ">" Then For Each cell In rng.Cells If cell.Value > VL Then Count = Count + 1 End If Next cell ElseIf CH = "<" Then For Each cell In rng.Cells If cell.Value < VL Then Count = Count + 1 End If Next cell Else For Each cell In rng.Cells If cell.Value = x Then Count = Count + 1 End If Next cell End If countifUDF = Count End Function 

在这个例子中, CH是第二个参数的第一个字符, VL是第二个参数的数字部分。

尝试这个:

 Function countifUDF(rng As Range, x As Integer) Dim cell As Range Dim Count As Integer Count = 0 For Each cell In rng.Cells If cell.Value < x Then Count = Count + 1 Next cell countifUDF = Count End Function