Search code examples
vbaloopsms-projectmsgbox

Popup MsgBox only once after 1 iteration


I want to have popup window only to show once regardless the number of tpred in this code. How to get on one popup window with all the examples from the loop. Now I need to confirm with Yes button each finding.

The macro is used in MS Project to trigger the task and to check if the predesessor is complete. If there is more then one predessecor, the popup appears with each predecessor.

Im very poor in VBA so will appreciate your help.

For Each T In ActiveSelection.Tasks

    reportingTeam = T.Text3
    RICEF = T.Text21
    UniqueID = T.UniqueID
    TaskName = T.Name
    completion = T.PercentComplete
    Duration = T.Duration / 60 & " hour/s"
    Start = DatePart("d", T.Start) & "." & DatePart("m", T.Start) & "." & DatePart("yyyy", T.Start)
    Finish = DatePart("d", T.Finish) & "." & DatePart("m", T.Finish) & "." & DatePart("yyyy", T.Finish)
    primaryres = T.Text4
    DataTeamResp = T.Text23
    recip2 = T.Text25
    reso = T.ResourceNames
    SPOC = T.Text10

    ' check if task isnt trigered?
    If completion > 0 Then
        MsgBox ("Task already triggered")
        Exit Sub
    End If

    For Each tpred In T.PredecessorTasks

        compl = compl & tpred.PercentComplete
        prednewid = prednewid & vbNewLine & tpred.UniqueID & "<br />"
        prednnazwa = prednnazwa & vbNewLine & tpred.Name & "<br />"
        predres = predres & vbNewLine & tpred.ResourceNames & "<br />"
        predSPOC = predSPOC & vbNewLine & tpred.Text10 & "<br />"
        predcomp = predcomp & vbNewLine & tpred.PercentComplete & " %" & "<br />"

        If compl <> 100 Then

            info = info & vbNewLine & tpred.UniqueID & " -> " & tpred.Name & " -> " & tpred.PercentComplete & " %" & " -> " & tpred.ResourceNames

            If MsgBox("For the:   " & T.UniqueID & "  ->  " & T.Name & vbNewLine & "following predecessors are not complete: " & vbNewLine & info & vbNewLine & vbNewLine & "Do you still want to trigger this task?", vbYesNo) = vbNo Then

                info = ""
                Exit Sub

            End If

        End If

    Next tpred

    T.PercentComplete = 5
    completion = T.PercentComplete / 100

Next T

Solution

  • Add a variable to track whether all predecessors are complete and move the message box for incomplete predecessors outside the loop:

    Dim AllPredsComplete As Boolean
    AllPredsComplete = True
    
    For Each tpred In T.PredecessorTasks
    
        prednewid = prednewid & vbNewLine & tpred.UniqueID & "<br />"
        prednnazwa = prednnazwa & vbNewLine & tpred.Name & "<br />"
        predres = predres & vbNewLine & tpred.ResourceNames & "<br />"
        predSPOC = predSPOC & vbNewLine & tpred.Text10 & "<br />"
        predcomp = predcomp & vbNewLine & tpred.PercentComplete & " %" & "<br />"
    
        If tpred.PercentComplete < 100 Then
            AllPredsComplete = False
            info = info & vbNewLine & tpred.UniqueID & " -> " & tpred.Name & " -> " _
                & tpred.PercentComplete & " %" & " -> " & tpred.ResourceNames
        End If
    
    Next tpred
    
    If Not AllPredsComplete Then
    
        If MsgBox("For the:   " & T.UniqueID & "  ->  " & T.Name & vbNewLine _
            & "following predecessors are not complete: " & vbNewLine & info & vbNewLine _
            & vbNewLine & "Do you still want to trigger this task?", vbYesNo) = vbNo Then
    
            info = ""
            Exit Sub
    
        End If
    
    End If