vba - 检查多个条件并存储值
问题描述
我有一个让我发疯的问题。它应该很简单,因为我以前做过很多次,但由于某些奇怪的原因它现在不起作用......
背景
我正在对某些列进行一些检查。如果发现每一列中的值为 True,则布尔标记将切换为 False。
有 3x 列要检查和 3x 布尔标记。
最后,我检查这些布尔标记的状态并获得输出。
代码
Dim TfPCheck as boolean
Dim CentreV as boolean
Dim FlaggedTasks as boolean
Dim AddtionalInfoCheck As Boolean
TfPCheck= True
CentreV = True
FlaggedTasks = True
AdditionalInfoCheck = True
With Worksheets("Admin")
For i = 7 To LR
If .Cells(i, 12) = "True" Then
TfPCheck = False
Exit For
End If
Next i
End With
With Worksheets("Admin")
For i = 7 To LR
If .Cells(i, 14) = "True" Then
CentreV = False
Exit For
End If
Next i
End With
With Worksheets("Admin")
For i = 7 To LR
If .Cells(i, 16) = "True" Then
FlaggedTasks = False
Exit For
End If
Next i
End With
If TfPCheck = True And CentreV = True And FlaggedTasks = True Then
AdditionalInfoCheck = True
ElseIf TfPCheck = False And CentreV = True And FlaggedTasks = True Then
addtionalInfoCheck = False
ElseIf TfPCheck = True And CentreV = False And FlaggedTasks = True Then
addtionalInfoCheck = False
ElseIf TfPCheck = True And CentreV = True And FlaggedTasks = False Then
addtionalInfoCheck = False
ElseIf TfPCheck = False And CentreV = True And FlaggedTasks = False Then
addtionalInfoCheck = False
ElseIf TfPCheck = False And CentreV = False And FlaggedTasks = True Then
addtionalInfoCheck = False
ElseIf TfPCheck = True And CentreV = False And FlaggedTasks = False Then
addtionalInfoCheck = False
ElseIf TfPCheck = False And CentreV = False And FlaggedTasks = False Then
addtionalInfoCheck = False
End If
MsgBox (AdditionalInfoCheck)
难道我做错了什么?因为 AddtionalInfoCheck 在不应该返回时返回 True。
谢谢
解决方案
我不知道这是否能解决您的问题,但似乎您的所有代码都可以归结为以下内容。
Dim TfPCheck As Boolean, CentreV As Boolean, FlaggedTasks As Boolean
Dim LR As Long, AdditionalInfoCheck As Boolean
With Worksheets("Admin")
LR = Application.Max(.Cells(.Rows.Count, "L").End(xlUp).Row, _
.Cells(.Rows.Count, "N").End(xlUp).Row, _
.Cells(.Rows.Count, "P").End(xlUp).Row)
TfPCheck = IsError(Application.Match(True, .Range(.Cells(7, "L"), .Cells(LR, "L")), 0))
CentreV = IsError(Application.Match(True, .Range(.Cells(7, "N"), .Cells(LR, "N")), 0))
FlaggedTasks = IsError(Application.Match(True, .Range(.Cells(7, "P"), .Cells(LR, "P")), 0))
End With
AdditionalInfoCheck = CBool(TfPCheck And CentreV And FlaggedTasks)
使用选项显式。AdditionalInfoCheck 有两种不同的拼写方式。
推荐阅读
- kubernetes - Kubernetes master 不附加 FlexVolume
- javascript - 是否可以将 Proptypes 用于复杂对象的关联数组
- visual-studio-2017 - 在 Visual Studio 中将 CSV 文件添加到项目中
- javascript - 用 JavaScript 替换除最后 4 个字符以外的字符
- r - 如何将变量添加到图例而不将其包含在图表中?
- python - 尝试在 Azure 中托管的数据库中插入带有 pyodbc 和 python 的字典列表时出错
- android - 找不到 Gradle DSL 方法:'versionCode()'
- java - 有没有办法在某些特定索引处用字符填充字符串?
- bash - 如何获取 find 命令的最后一个结果?
- javascript - 为什么我不能从 JS 继承中的子类实例访问父类的属性?