首页 > 解决方案 > 检查多个条件并存储值

问题描述

我有一个让我发疯的问题。它应该很简单,因为我以前做过很多次,但由于某些奇怪的原因它现在不起作用......

背景

我正在对某些列进行一些检查。如果发现每一列中的值为 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。

谢谢

标签: vbaexcel

解决方案


我不知道这是否能解决您的问题,但似乎您的所有代码都可以归结为以下内容。

    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 有两种不同的拼写方式。


推荐阅读