[英]VBA check if columns contains specific value
如何查找列中是否包含TRUE或FALSE VALUE?
As an example, you could evaluate cell A1
like this: 例如,您可以这样评估单元格
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
Here is an alternative solution, since perhaps the goal is more of a "T/F Summary" for the entire column: 这是一个替代解决方案,因为目标可能是整个专栏的“ 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
=IFERROR(LOOKUP(TRUE,A:A),IFERROR(NOT(LOOKUP(FALSE,A:A)),"NEITHER FOUND"))
Or 要么
=IFERROR(IF(LOOKUP(TRUE,A:A),TRUE,IF(NOT(LOOKUP(FALSE,A:A)),FALSE)),"NEITHER FOUND")
Or 要么
IF(SUMPRODUCT(--(A:A=TRUE))>0,"True found",IF(SUMPRODUCT(--(A:A=FALSE))>0,"FALSE FOUND",))
Or 要么
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
As a function call (to allow you an easy way to use as UDF in a sheet passing a column in as range argument): 作为函数调用(为您提供一种在工作表中传递列作为范围参数的工作表中用作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
I think the first two answers posted are too complex based on the succinct requirements the OP lists. 我认为,基于OP列出的简洁要求,发布的前两个答案过于复杂。 A
CountIF
approach seems like the most efficient approach. CountIF
方法似乎是最有效的方法。
Ignoring the OP title, the answer does not even require VBA. 忽略OP标题,答案甚至不需要VBA。 It can just be a cell formula:
它可以只是一个单元格公式:
Test if True Found: 测试是否找到True:
=IF(COUNTIF(B:B,TRUE)>0,"There's a TRUE in the Column!","No true's found!")
Test if True OR False found: 测试是否找到对或错:
=IF(COUNTIF(B:B,TRUE)+COUNTIF(B:B,FALSE)>0,"There's either a True or False FOUND!","Nothing found")
If this activity does need to be referenced in VBA, then you can use a similar approach. 如果确实需要在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
Here's a simple one-liner, which returns true if a match was found and false if no match was found: 这是一个简单的单行代码,如果找到匹配项,则返回true;如果没有找到匹配项,则返回false:
Debug.Print Not IsError(Application.Match(True, Range("A:A"), 0))
The code above checks for the boolean value True
, but if you want to check for the string "True", simply insert "True" in the first parameter instead. 上面的代码检查布尔值
True
,但是如果要检查字符串“ True”,只需在第一个参数中插入“ True”即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.