PDA

View Full Version : Pop up messages when many programs are executed in VBA PowerPoint



CuriosityBug
09-05-2019, 04:17 AM
Hello All,

I am working on UI form which has multiple check boxes. My goal is to display a popup message when all check boxed items are executed successfully. As shown below, if "apple" and "spinach" were checked and " okay" button is clicked, a popup message saying

" Apple and spinach are successful"

If "spinach" fails to run successfully, then it could popup error message and also successful execution of "apple". Excuse me if this is vague explanation of the problem.

Please help:)

Paul_Hossler
09-05-2019, 07:20 AM
What causes the UF to display?

Is this during a slideshow?

I assume that Apple, Orange, and Spinach are 3 macros


Something like this in a standard module



Option Explicit


Sub drv()
Load UserForm1
UserForm1.Show

End Sub




Function Apple() As Variant
If Rnd < 0.5 Then
Apple = True
Else
Apple = False
End If
End Function


Function Orange() As Variant
If Rnd < 0.75 Then
Orange = True
Else
Orange = False
End If
End Function


Function Spinach() As Variant
If Rnd < 0.95 Then
Spinach = True
Else
Spinach = False
End If
End Function





and this in the UF




Option Explicit




Private Sub btnOK_Click()
Dim bApple As Long, bOrange As Long, bSpinach As Long
Dim sMsg As String

bApple = 1
bOrange = 1
bSpinach = 1

With Me
If .cbApple.Value Then bApple = Apple
If .cbOrange.Value Then bOrange = Orange
If .cbSpinach.Value Then bSpinach = Spinach
End With

If bApple <> 1 Then sMsg = sMsg & IIf(bApple, "Apple ran successfully", "Apple failed") & vbCrLf
If bOrange <> 1 Then sMsg = sMsg & IIf(bOrange, "Orange ran successfully", "Orange failed") & vbCrLf
If bSpinach <> 1 Then sMsg = sMsg & IIf(bSpinach, "spinach ran successfully", "Spinach failed") & vbCrLf

Call MsgBox(sMsg, vbOKOnly + vbInformation, "Apple-Orange-Spinach")


Me.Hide

Unload Me


End Sub


Private Sub UserForm_Initialize()
With Me
.cbApple.Value = False
.cbOrange.Value = False
.cbSpinach.Value = False
End With
End Sub

CuriosityBug
09-05-2019, 07:40 AM
Hello Paul.

I created a button on one of slides. So, if I click on this button, it displays UI form. Yes, all check box items are separate macros return in a module. When user clicks on "Okay" button, all macros that were checked run.

Something like :
Sub Apple()
--- other code here--
MsgBox " Apple is successful "
End Sub

Paul_Hossler
09-05-2019, 07:58 AM
I edited my original reply to add some concept macros and an attachment

Give them a test and see if you can use the ideas

CuriosityBug
09-05-2019, 12:05 PM
Thank you for the insights, I am working on it and will soon let you know once I achieve it. Is it possible to display error message instead " failed"? Just to let the user be aware of the reason behind failure.

Paul_Hossler
09-05-2019, 01:19 PM
Here's one way





Option Explicit


Sub drv()
Load UserForm1
UserForm1.Show

End Sub




Function Apple() As String
If Rnd <= 0.5 Then
Apple = "Success: Apple <= 0.5"
Else
Apple = "Fail: Apple > 0.5"
End If
End Function


Function Orange() As String
If Rnd <= 0.75 Then
Orange = "Success: Orange <= 0.75"
Else
Orange = "Fail: Orange > 0.75"
End If
End Function


Function Spinach() As String
If Rnd <= 0.95 Then
Spinach = "Success: Spinach <= 0.95"
Else
Spinach = "Fail: Spinach > 0.95"
End If
End Function







Option Explicit




Private Sub btnOK_Click()
Dim sApple As String, sOrange As String, sSpinach As String
Dim sMsg As String

sApple = vbNullString
sOrange = vbNullString
sSpinach = vbNullString

With Me
If .cbApple.Value Then sApple = Apple
If .cbOrange.Value Then sOrange = Orange
If .cbSpinach.Value Then sSpinach = Spinach
End With

If Len(sApple) > 1 Then sMsg = sMsg & sApple & vbCrLf
If Len(sOrange) > 1 Then sMsg = sMsg & sOrange & vbCrLf
If Len(sSpinach) > 1 Then sMsg = sMsg & Spinach & vbCrLf

Call MsgBox(sMsg, vbOKOnly + vbInformation, "Apple-Orange-Spinach")


Me.Hide

Unload Me


End Sub


Private Sub UserForm_Initialize()
With Me
.cbApple.Value = False
.cbOrange.Value = False
.cbSpinach.Value = False
End With
End Sub

CuriosityBug
09-05-2019, 01:33 PM
is it possible to do something like:

Function Apple() As Variant
--code--
If code runs well then it is successful. if error comes it is " failed".

If code = successful Then
Apple = True
Else
Apple = False ' when error occurred anywhere in the code
End If

End Function

what if the code doesn't have IF condition to compare anything with. Here the comparison is whole code executed. Not sure if this practical. Any insights which can guide me.:)

CuriosityBug
09-05-2019, 01:39 PM
Something like code below which doesn't have any conditional statements to compare with.



Sub changecol(oshp As Shape)

oshp.Fill.ForeColor.RGB = RGB(255, 0, 0)

End Sub

Paul_Hossler
09-05-2019, 02:16 PM
?

That's usually the macro that gets run when a shape is clicked


Is that (for example), 'Apple' ?

CuriosityBug
09-05-2019, 03:09 PM
I mean, if 'changecol' run smoothly without any interruption (I took just an example to show the code which don't have If condition in it).

If I don't have functionality like this wihtin the code :

If Rnd < 0.75 Then ' something to compare and decide value of orange as true.
Orange = True
Else
Orange = False
End If

CuriosityBug
09-05-2019, 03:36 PM
Hello Paul,

I achieved it! Thank you so much for your patience. I played with the code snippet you provided at beginning. It took little time to understand:)

Paul_Hossler
09-05-2019, 03:40 PM
It's a little hard to test with no If's, but do you mean something like this?

It's preset to False, and if there is an issue, it just exits

Only if it goes all the way does it return True





Option Explicit


Sub drv()
Load UserForm1
UserForm1.Show

End Sub




Function Apple() As Variant
Apple = False

If Rnd < 0.25 Then Exit Function

Apple = True

End Function


Function Orange() As Variant
Orange = False

If Rnd < 0.5 Then Exit Function

Orange = True

End Function


Function Spinach() As Variant
Spinach = False

If Rnd < 0.95 Then Exit Function

Spinach = True

End Function




Another way




Option Explicit


Sub drv()
Load UserForm1
UserForm1.Show

End Sub




Function Apple() As Variant

Apple = True

On Error GoTo ReturnFalse

If Rnd < 0.25 Then Err.Raise 500, "Apple", "Rotten Apple"

Exit Function

ReturnFalse:
Apple = False

End Function
Function Orange() As Variant

Orange = True

On Error GoTo ReturnFalse

If Rnd < 0.5 Then Err.Raise 501, "Orange", "Rotten Orange"

Exit Function

ReturnFalse:
Orange = False

End Function
Function Spinach() As Variant

Spinach = True

On Error GoTo ReturnFalse

If Rnd < 0.75 Then Err.Raise 502, "Spinach", "Rotten Spinach"

Exit Function

ReturnFalse:
Spinach = False

End Function

CuriosityBug
09-05-2019, 03:48 PM
I tried something like this: It worked so far, I should test with multiple test cases.:)

Function changecol(oshp As Shape)


oshp.Fill.ForeColor.RGB = RGB(255, 0, 0)
changecol = True
Exit Function

Err_Handler:
changecol = False

End Function

Paul_Hossler
09-05-2019, 03:53 PM
Where does the UF come into play?

CuriosityBug
09-05-2019, 04:04 PM
I modified code in UI as well. But, the above is main functionality I am looking for. Added extra UI button "changecol" under check boxes and just modified your code.