How to write 2 codes in Private Sub Workbook_Open()

raajnabriz

Board Regular
Joined
Jun 11, 2014
Messages
95
hi,

i would like to write 2 codes in Private Sub Workbook_Open() as follows [ or rather how to combine them together]

The first code is to force enable macros and the second is message box upon workbook open.

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Step 1: Declare your variables
Dim ws As Worksheet
'Step 2: Unhide the Starting Sheet
Sheets("START").Visible = xlSheetVisible
'Step 3: Start looping through all worksheets
For Each ws In ThisWorkbook.Worksheets
'Step 4: Check each worksheet name
If ws.Name <> "START" Then
'Step 5: Hide the sheet
ws.Visible = xlVeryHidden
End If
'Step 6: Loop to next worksheet
Next ws
'Step 7: Save the workbook
ActiveWorkbook.Save
End Sub


Private Sub Workbook_Open()
'Step 1: Declare your variables
Dim ws As Worksheet
'Step 2: Start looping through all worksheets
For Each ws In ThisWorkbook.Worksheets
'Step 3: Unhide All Worksheets
ws.Visible = xlSheetVisible
'Step 4: Loop to next worksheet
Next ws
'Step 5: Hide the Start Sheet
Sheets("START").Visible = xlVeryHidden
End Sub

AND

Code:
Private Sub Workbook_Open()
MsgBox "Created By: RAAJNABRIZ" & vbCrLf & "M. C. Department", vbOKOnly + vbInformation
End Sub

when i use the above codes, it does not work bcoz there are 2 workbook_Open() and if i write both codes in one workbook_Open() it also does not work, please help

any help will be appreciated
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

jtakw

Well-known Member
Joined
Jun 29, 2014
Messages
7,245
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Don't really see a reason why it wouldn't work combined...

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
 'Step 1: Declare your variables
 Dim ws As Worksheet
 'Step 2: Unhide the Starting Sheet
 Sheets("START").Visible = xlSheetVisible
 'Step 3: Start looping through all worksheets
 For Each ws In ThisWorkbook.Worksheets
 'Step 4: Check each worksheet name
 If ws.Name <> "START" Then
 'Step 5: Hide the sheet
 ws.Visible = xlVeryHidden
 End If
 'Step 6: Loop to next worksheet
 Next ws
 'Step 7: Save the workbook
 ActiveWorkbook.Save
End Sub


Private Sub Workbook_Open()
 
 'Show message box, credits
 
 MsgBox "Created By: RAAJNABRIZ" & vbCrLf & "M. C. Department", vbOKOnly + vbInformation
 
 'Step 1: Declare your variables
 Dim ws As Worksheet
 'Step 2: Start looping through all worksheets
 For Each ws In ThisWorkbook.Worksheets
 'Step 3: Unhide All Worksheets
 ws.Visible = xlSheetVisible
 'Step 4: Loop to next worksheet
 Next ws
 'Step 5: Hide the Start Sheet
 Sheets("START").Visible = xlVeryHidden
End Sub
 
Last edited:

rediffusion

Board Regular
Joined
Apr 24, 2019
Messages
60
jtakw

I have some problem, please HELP!

How to make work this with other codes? I want to open my own created tab with starting «Excel». My own tab is called `My Macros`.
Code:
SendKeys "%BP{F6}"
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,164
Messages
5,835,753
Members
430,384
Latest member
kiuwai

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