Message box help

Stevetron

New Member
Joined
Jul 9, 2020
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Hi,
I am trying to write some VBA code. I want to have a message box appear every time a workbook is opened.

I want it to have a yes/no button, where pressing yes will show "sheet1" and pressing no will show "sheet2".

So far i have this:

Private Sub Workbook_Open()
Dim answer As Integer
answer = MsgBox("Is this your first time using this spreadsheet?", vbYesNo + vbQuestion, "Instructions")
If answer = vbYes Then
Worksheets("Sheet1").Activate
Else
Worksheets("Sheet2").Activate
End If
End Sub

The message box appears when i press play in VBA, but it does not open automatically when i open the workbook, nor does it return sheet 1 when yes is pressed, or return sheet2 when no is pressed.

I am an inexperienced vba user.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try this:

VBA Code:
Private Sub Workbook_Open()
Dim answer As VbMsgBoxResult
answer = MsgBox("Is this your first time using this spreadsheet?", vbYesNo + vbQuestion, "Instructions")
If answer = vbYes Then
Worksheets("Sheet1").Activate
Else
Worksheets("Sheet2").Activate
End If
End Sub
 
Upvote 0
The code looks fine to me. Is it in the right place? It should be in the ThisWorkbook module. Double click ThisWorkbook for your project and the code should appear at the right.

1595838131403.png
 
Upvote 0
Thank you both for your responses.

I changed the code to Trevor's. - could you explain why you changed line2 of the code please?

Peter you were correct, i had it as a module, one i changed it to 'this workbook' all is good.
 
Upvote 0
Peter you were correct, i had it as a module, one i changed it to 'this workbook' all is good.
Good news! Thanks for the confirmation.

In this particular case it shouldn't matter Dim answer as Integer or VbMsgBoxResult (or Long)
 
Upvote 0
By using this it looks at various options within MsgBox buttons look here (hope it helps):


And here (Scroll down) this is displayed when you highlight vbMsgResult and press F1 for Help.


Contains constants used to identify which button was pressed on a message box displayed by using the MsgBox function. These constants can be used anywhere in your code.
 
Upvote 0
Thank you Trevor. Knowledge is key, and i am trying to gain as much as possible.

Much appreciated.
 
Upvote 0
Your welcome and thank you for the reply. Do note what Peter_SSs has written in his response to.
 
Upvote 0

Forum statistics

Threads
1,213,561
Messages
6,114,317
Members
448,564
Latest member
ED38

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