UDF를 어떻게 작성하여 인수에 비교 연산자를 사용할 수 있습니까?VBA Excel 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
이 = COUNTIF 같은 함수에 비교 연산자를 전달할 수 있습니다 (범위; "<"& X)이고 x보다 작은 범위의 셀 수가 표시됩니다.
어떻게하면 UDF에서이 작업을 수행 할 수 있습니까? = countifUDF로 내 UDF (범위; "<"& 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에서 비교 연산자를 처리 할 때, 내가 틀렸다면 나를 바로 잡으십시오. 내 솔루션은 와일드 카드로 숫자와 문자열을 모두 지원합니다. 처음에는 분리 방법으로 &으로 분할 방법을 사용하려고했습니다. Appearantly VBA를 식별 ' ">"& X'로 '> X'나는이 시도 문자로 X 문자를 분할하고 사용자가에 입력 비교 연산자의 종류 평가
고마워요! 나는 해결책이 이렇게 보일 것이라고 생각했지만 실제로 <>,> =, <=, <, > 또는 =을 더 편리한 방법으로 전달하는 어떤 방법이 존재하기를 바라고있었습니다. 비슷한 해결책을 찾아 보겠지 만 <>, <= and > =을 처리하는 것이 더 쉬우므로 구분 기호로 &로 분할을 사용합니다. –
@ user3805500 귀하의 아이디어는 흥미 롭습니다. ............ 최종 접근 방식으로 질문을 업데이트하면 감사하겠습니다! –
질문에 내 솔루션을 게시, 꽤 aint. –