Home > Software design >  How to have Msgbox appear or not appear selectively
How to have Msgbox appear or not appear selectively

Time:03-29

Sub Process_A()
\<some code here\>
MsgBox "Process completed."
End Sub

Sub Process_B()
\<some code here\>
MsgBox "Process completed."
End Sub

Sub Process_C()
\<some code here\>
MsgBox "Process completed."
End Sub

Sub All_Processes()
Call Process_A
Call Process_B
Call Process_C
End Sub

This is some code I have using Excel VBA.

I want MsgBox to appear when I run Process_A, Process_B or Process_C individually, but this also meant that when I run All_Processes, MsgBox appears 3 times.

Is there a way for me to run All_Processes and have Msgbox appear only once after Process_C is completed? Thank you.

CodePudding user response:

You could create an optional Boolean parameter for each process that controls whether or not it reports its completion.

Something like:

Sub ProcessA(Optional notify As Boolean = False)
    'some code
    If notify Then MsgBox "Process A completed."
End Sub

Sub ProcessB(Optional notify As Boolean = False)
    'some code
    If notify Then MsgBox "Process B completed."
End Sub

Sub ProcessC(Optional notify As Boolean = False)
    'some code
    If notify Then MsgBox "Process C completed."
End Sub

Sub AllProcesses()
    ProcessA
    ProcessB
    ProcessC True
End Sub

CodePudding user response:

Maybe change your procedures to functions that can return whether the code was successful or not.

Public Function Process_A() As String
    'Replace "Incomplete" with some code that
    'calculates whether procedure completed or not.
    Process_A = "Incomplete"
End Function

Public Function Process_B() As String
    Process_B = "Complete"
End Function

Public Function Process_C() As String
    Process_C = "Complete"
End Function

Public Sub All_Processes()

    Dim resp As String
    
    resp = "A: " & Process_A & vbCr & _
           "B: " & Process_B & vbCr & _
           "C: " & Process_C & vbCr
           
    MsgBox "Process results: " & vbCr & vbCr & resp

End Sub
  • Related