How to write 2 codes in Private Sub Workbook_Open()

raajnabriz

Board Regular
Joined
Jun 11, 2014
Messages
112
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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
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:
Upvote 0
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}"
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

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