Msgbox yes / no code advice please

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Morning,
Im having an issue trying to get this to work correctly but now im confused with what ive done.
The code i have in use is supplied below.

This is how it should work.

I open the worksheet & the Msgbox reads ADD CUSTOMER TO WORKSHEET
Yes should then DatabaseUserForm.Show
No should then DatabaseNameSearch.Show

My issues are when closing a form the other is shown or if i select Yes then nothing is shown at all.


VBA Code:
Private Sub Worksheet_Activate()
Range("A5").Select
Range("A6").Select
Dim answer As Integer
 
answer = MsgBox("ADD NEW CUSTOMERS NAME TO WORKSHEET ?", vbQuestion + vbYesNo + vbDefaultButton2, "DATABASE NEW CUSTOMERS NAME MESSAGE")
If answer = vbNo Then
answer = MsgBox("SEARCH FOR CUSTOMER IN WORKSHEET ?", vbQuestion + vbYesNo + vbDefaultButton2, "DATABASE SEARCH FOR CUSTOMER MESSAGE")
Else
DatabaseUserForm.Show
If answer = vbYes Then
DatabaseNameSearch.Show
Else
DatabaseUserForm.Show
End If
End If
    
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
See if this does what you need
VBA Code:
Private Sub Worksheet_Activate()
Select Case MsgBox "ADD NEW CUSTOMERS NAME TO WORKSHEET ?", vbQuestion + vbYesNo + vbDefaultButton2, "DATABASE NEW CUSTOMERS NAME MESSAGE"
    Case vbNo
        DatabaseUserForm.Show
    Case vbYes
        DatabaseNameSearch.Show
End Select
End Sub
 
Upvote 0
How about
VBA Code:
Private Sub Worksheet_Activate()
 
If MsgBox("ADD NEW CUSTOMERS NAME TO WORKSHEET ?", vbYesNo, "DATABASE NEW CUSTOMERS NAME MESSAGE") = vbYes Then
    DatabaseUserForm.Show
Else
    If MsgBox("SEARCH FOR CUSTOMER IN WORKSHEET ?", vbYesNo, "DATABASE SEARCH FOR CUSTOMER MESSAGE") = vbYes Then DataBaseNameSearch.Show
End If
    
End Sub
 
Upvote 0
The second code worked BUT see my long winded working code whilst waiting for a reply.


VBA Code:
Private Sub Worksheet_Activate()
Range("A5").Select
Range("A6").Select
Dim answer As Integer
 
answer = MsgBox("ADD NEW CUSTOMERS NAME TO WORKSHEET ?", vbQuestion + vbYesNo + vbDefaultButton2, "DATABASE NEW CUSTOMERS NAME MESSAGE")
If answer = vbYes Then
DatabaseUserForm.Show
Else
If answer = vbNo Then
answer = MsgBox("SEARCH FOR CUSTOMER IN WORKSHEET ?", vbQuestion + vbYesNo + vbDefaultButton2, "DATABASE SEARCH FOR CUSTOMER MESSAGE")
If answer = vbYes Then
DatabaseNameSearch.Show
Else
If answer = cbNo Then
Exit Sub
End If
End If
End If
End If
End Sub
 
Upvote 0
Select Case line is in Red
Oops, my bad, I shouldn't have removed the parentheses
VBA Code:
Private Sub Worksheet_Activate()
Select Case MsgBox("ADD NEW CUSTOMERS NAME TO WORKSHEET ?", vbQuestion + vbYesNo + vbDefaultButton2, "DATABASE NEW CUSTOMERS NAME MESSAGE")
    Case vbNo
        DatabaseUserForm.Show
    Case vbYes
        DatabaseNameSearch.Show
End Select
End Sub
What should happen if the user tries to close the message box without selecting yes or no?
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top