vba code shows every message box when its only suppose to show one message box a
ID: 3852353 • Letter: V
Question
vba code shows every message box when its only suppose to show one message box at a time when the user selects their preference. can I have help fixing this. please leave a copy code or comments with how to fix it
Private Sub btnOk_Click()
Dim Recommend As String
Dim Popularity As String
Dim msg As String
If OptProfessor = True And optMost = True Then
Recommend = "By Professor"
Popularity = "Most Popular Song"
End If
MsgBox " Professors recommend YellowBrick Cinema as the most popular song by 60%."
If OptProfessor = True And OptLeast = True Then
Recommend = "By Professor"
Popularity = "Least Popular"
End If
MsgBox " Professors recommend JaBig as the least popoluar song by 85%."
If OptStudent = True And optMost = True Then
Recommend = "By Student"
Popularity = "Most Popular Song"
End If
MsgBox " Students recommend Instrumental Music as the most popular song by 65%."
If OptStudent = True And OptLeast = True Then
Recommend = "By Student"
Popularity = "Least Popular"
End If
MsgBox " Students recommend JaBig as the least popular song by 75%."
If OptAnonymous = True And optMost = True Then
Recommend = "By Anonymous"
Popularity = "Most Popular Song"
End If
MsgBox " Anonymous recommend YellowBrick Cinema as the most popular song by 90%."
If OptAnonymous = True And OptLeast = True Then
Recommend = "By Anonymous"
Popularity = "Least Popular"
End If
MsgBox " Anonymous recommend JaBig as the least popular song by 10%."
ireply = MsgBox(msg, vbQuestion + vbOKCancel)
Select Case ireply
Case vbCancel
MsgBox "Sorry, you don’t want to listen."
Exit Sub
End Select
Explanation / Answer
Solution)
You need to replace each individual if statement with an if-else-if ladder. The problem is that when a condition is satisfied the message box related to that if statement is displayed as well as the condition with each remaining if's are also matched and the body associated to those if's are displayed as well. Hence you need to replace individual if statement with if - else if ladder so that only one condition is executed at a time. Probably this would solve your problem and if not then you need to post the excel sheet and the complete problem.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.