VBA检查列是否包含特定值

时间:2018-07-21 16:33:25

标签: excel vba excel-vba boolean

如何查找列是否包含TRUE或FALSE值?

4 个答案:

答案 0 :(得分:4)

例如,您可以像这样评估单元格A1

Sub Demo()
    Select Case Sheets("Sheet1").Range("A1")
        Case True
            MsgBox "Cell A1 is TRUE"
        Case False
            MsgBox "Cell A1 is FALSE"
        Case Else
            MsgBox "Cell A1 is neither True nor False"
    End Select
End Sub

编辑:

这里是一个替代解决方案,因为目标可能更多地是 整个 列的“ T / F摘要”:

Sub Demo2()

    Dim cntT As Long, cntF As Long, resp As String
    cntT = WorksheetFunction.CountIf(Sheets("Sheet1").Columns("A"), True)
    cntF = WorksheetFunction.CountIf(Sheets("Sheet1").Columns("A"), False)

    Select Case cntT
        Case Is > 0
            Select Case cntF
                Case Is > 0
                    resp = "a mix of " & cntT & " TRUE and " & cntF & " FALSE."
                Case Else
                    resp = cntT & " TRUE but no FALSE."
            End Select
        Case Else
            Select Case cntF
                Case Is > 0
                    resp = cntF & " FALSE but no TRUE."
                Case Else
                    resp = "neither TRUE nor FALSE."
            End Select
    End Select

    MsgBox "The column contains " & resp, vbInformation

End Sub

示例输出:

img

答案 1 :(得分:1)

=IFERROR(LOOKUP(TRUE,A:A),IFERROR(NOT(LOOKUP(FALSE,A:A)),"NEITHER FOUND"))

=IFERROR(IF(LOOKUP(TRUE,A:A),TRUE,IF(NOT(LOOKUP(FALSE,A:A)),FALSE)),"NEITHER FOUND")

IF(SUMPRODUCT(--(A:A=TRUE))>0,"True found",IF(SUMPRODUCT(--(A:A=FALSE))>0,"FALSE FOUND",))

Option Explicit
Public Sub Demo()
    Dim arr(), i As Long
    Const COL_NUMBER As Long = 1                     '< =Col A
    arr = Array(True, False)
    For i = LBound(arr) To UBound(arr)
        If Not ActiveSheet.Columns(COL_NUMBER).Find(arr(i)) Is Nothing Then
            MsgBox arr(i) & " found in column " & COL_NUMBER
            Exit Sub
        End If
    Next i
    MsgBox "Neither True not False Found"
End Sub

作为函数调用(以使您可以轻松地在将列作为范围参数传递的工作表中用作UDF)

Option Explicit
Public Sub Test()
    Dim rng As Range
    Const COL_NUMBER As Long = 1     '< =Col A
    Set rng = ActiveSheet.Columns(COL_NUMBER)
    MsgBox IsTrueOrFalseFound(rng)
End Sub
Public Function IsTrueOrFalseFound(ByVal rng As Range) As String
    Dim arr(), i As Long
    arr = Array(True, False)
    For i = LBound(arr) To UBound(arr)
        If Not rng.Find(arr(i)) Is Nothing Then
            IsTrueOrFalseFound = arr(i) & " found in column " & rng.Column
            Exit Function
        End If
    Next i
    IsTrueOrFalseFound = "Neither True not False Found"
End Function

答案 2 :(得分:1)

基于OP列出的简洁要求,我认为发布的前两个答案过于复杂。 CountIF方法似乎是最有效的方法。

忽略OP标题,答案甚至不需要VBA。它可以只是一个单元格公式:

测试是否找到True:

=IF(COUNTIF(B:B,TRUE)>0,"There's a TRUE in the Column!","No true's found!")

测试是否找到True或False:

=IF(COUNTIF(B:B,TRUE)+COUNTIF(B:B,FALSE)>0,"There's either a True or False FOUND!","Nothing found")

如果确实需要在VBA中引用此活动,则可以使用类似的方法。

Dim SrchRNG As Range
Set SrchRNG = Range("B:B")

If Application.WorksheetFunction.CountIf(SrchRNG, True) + Application.WorksheetFunction.CountIf(SrchRNG, False) > 0 Then
    'Here's when a true or false has been found
Else
    'Code if neither is found
End If

答案 3 :(得分:0)

这是一个简单的单行代码,如果找到匹配项,则返回true,如果找不到匹配项,则返回false:

Debug.Print Not IsError(Application.Match(True, Range("A:A"), 0))

上面的代码检查布尔值True,但是如果要检查字符串“ True”,只需在第一个参数中插入“ True”。