problem combining workbook open code

vbnoob

Board Regular
Joined
Oct 22, 2008
Messages
67
Hi all,

I am trying to combine two seperate bits of code that I have in diffrent workbooks in a new one I am doing. However I cannot seem to combine them under one Workbook open() functions. I cant do two sepearte ones as it gives me an ambiguous error.

Here are the two codes I am trying to combine. Please help, much in need on this one.

Code:
Private Sub Workbook_Open()
    Sheets("database").Select
   Range("au16").Select
   dtmSchedule = Now + TimeValue("00:15:00")
   Application.OnTime dtmSchedule, "TimeOut"
   If ThisWorkbook.ReadOnly Then
   MsgBox ("The file is currently being used by another user, please contact them to ensure they have not left the file open.")
   ActiveWorkbook.Close SaveChanges:=False
   End If
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
   On Error Resume Next
   Application.OnTime dtmSchedule, "TimeOut", , False
End Sub

Code:
Option Explicit
Private Sub Workbook_Open()
    With Worksheets("PP")
        .Protect Password:="bargy", userinterfaceonly:=True
        .EnableOutlining = True
    End With
    
    
End Sub

Thanks for the help everyone
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Just add the With/End With portion of the second Workbook_Open to the first Workbook_Open.

Code:
Private Sub Workbook_Open()
dtmSchedule = Now + TimeValue("00:15:00")
Application.OnTime dtmSchedule, "TimeOut"
If ThisWorkbook.ReadOnly Then
    MsgBox ("The file is currently being used by another user, please contact them to ensure they have not left the file open.")
    ThisWorkbook.Close SaveChanges:=False
End If
With Worksheets("PP")
    .Protect Password:="bargy", userinterfaceonly:=True
    .EnableOutlining = True
End With
End Sub
 
Upvote 0
I got it to work. there was still the Option explcit bit of code at the top. I removed that and it worked.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,246
Members
449,075
Latest member
staticfluids

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