检查多个条件并存储值

时间:2018-06-24 00:59:52

标签: excel vba excel-vba

我遇到一个让我发疯的问题。它应该很简单,因为我之前已经做过很多次了,但是由于某种奇怪的原因,它现在不起作用了。

背景

我正在对某些列进行一些检查。如果发现每个列中的值为True,则布尔标记将切换为False。

有3个要检查的列和3个布尔标记。

最后,我检查这些布尔标记的状态并获得输出。

代码

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的情况下返回。

谢谢

1 个答案:

答案 0 :(得分:1)

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

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