Password issues on Workbook Open Event

quackdad

Board Regular
Joined
Jul 23, 2013
Messages
110
When I run this code I get a message that the password is incorrect. But it is correct. Do I need to state it another way?
Code:
Private Sub Workbook_Open()
    Sheets("Employees").Unprotect Password = "Password"
    Range("F8").Select
    Sheets("Employees").Protect Password = "Password"
End Sub
 
The latter won't work. :)

You mean:
Code:
.Unprotect "Password"

There are two ways of passing arguments to routines - by name (Password:="Password") and by position (just "Password'). If you pass by name, you can pass the arguments in any order you like, and only include the ones you require; if you pass by position, you must pass in the order the routine expects them, and you must leave a gap for any optional ones you do not wish to pass.

With a method like Unprotect there is no real difference because there only is one argument - the password.

However, with a method like Workbooks.Open, there are 15 possible arguments, only the first of which is required - the filename. Of course if that's all you want to pass, there's no great benefit to passing by name, other than perhaps more legible code:
Code:
Workbooks.Open "C:\book1.xls"
versus:
Code:
Workbooks.Open FileName:="C:\book1.xls"

If you want to provide a password, however, which is the fifth argument, passing by name is still just:
Code:
Workbooks.Open FileName:="C:\book1.xls", Password:="blah"
whereas by position, you have to leave gaps for the optional arguments 2-4:
Code:
Workbooks.Open "C:\book1.xls", , , , "blah"

Not too bad, but let's say I want to open the workbook read-only and tell it what to do if it encounters an error while trying to load the workbook data. Using named arguments:
Code:
Workbooks.Open FileName:="C:\book1.xls", Password:="blah", ReadOnly:=True, CorruptLoad:=xlRepairFile

but passing by position it's:
Code:
Workbooks.Open "C:\book1.xls", , True, , "blah", , , , , , , , , , xlRepairFile

I know which one I think is easier to write - and to read 6 months later! :)

(I think I just drafted this week's blog entry ;))
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,215,220
Messages
6,123,698
Members
449,117
Latest member
Aaagu

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