2016-06-30 4 views
1

UDFを書くと、引数の比較演算子を受け入れることができますか?VBA Excel UDF引数で比較演算子を使用する方法?

= countif(range; x)標準関数を使用すると、xに等しい範囲のセル数が得られます。 (;「<」&のx範囲)

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 

標準関数を使用する場合は、あなたがこの= COUNTIFのような関数に比較演算子を渡すことができます:VBAでこの機能を複製

は、次のようになりますとなり、xより小さい範囲のセル数が得られます。

これをUDFでどのように行うことができますか? = countifUDFとしての私のUDF(範囲; "<" & x)のは、私はそれがVBAには便利な方法theresのように思えるんだ答えを考えると#VALUE


SOLUTION

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 

を生み出しますUDFでの比較演算子の処理については、私が間違っている場合は私を修正してください。私のソリューションは、ワイルドカードで数字と文字列の両方をサポートしています。最初に私は区切り文字として&とSplitメソッドを使用しようとしました。 。私は、文字でのx文字を分割し、で入力した比較演算子のどのようなユーザーを評価しなければならなかった理由Appearantly VBAが「> x」のよう「『>』 & x」の識別

答えて

2

この例では

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はセコの最初の文字である:UDFを持っている()文字列として2番目の引数は考えますnd引数とVLは2番目の引数の数値部分です。

+1

ありがとうございます!私は解決策がこれと似ていると思っていましたが、実際には<>、> =、<=, <, >または=をより便利な方法で渡す方法が存在することを期待していました。私は似たような解決策をとるが、<>、<= and > =を処理する方が簡単であるため、&と区切り文字を区切り文字として使用します。 –

+0

@ user3805500あなたのアイデアは面白いです............あなたの最終的なアプローチであなたの質問を更新すれば、それは認められるでしょう! –

+0

質問に私の解決策を掲載しました。 –

1

これを試してみてください:

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 
関連する問題