Message box help

Stevetron

New Member
Joined
Jul 9, 2020
Messages
26
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.
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Trevor G

Well-known Member
Joined
Jul 17, 2008
Messages
6,701
Office Version
  1. 2016
Platform
  1. Windows
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
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
47,926
Office Version
  1. 365
Platform
  1. Windows
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
 

Stevetron

New Member
Joined
Jul 9, 2020
Messages
26
Office Version
  1. 365
Platform
  1. Windows
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.
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
47,926
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

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)
 

Trevor G

Well-known Member
Joined
Jul 17, 2008
Messages
6,701
Office Version
  1. 2016
Platform
  1. Windows
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.
 

Stevetron

New Member
Joined
Jul 9, 2020
Messages
26
Office Version
  1. 365
Platform
  1. Windows
Thank you Trevor. Knowledge is key, and i am trying to gain as much as possible.

Much appreciated.
 

Trevor G

Well-known Member
Joined
Jul 17, 2008
Messages
6,701
Office Version
  1. 2016
Platform
  1. Windows
Your welcome and thank you for the reply. Do note what Peter_SSs has written in his response to.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,567
Messages
5,596,908
Members
414,110
Latest member
docops

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
Top