Get Password text for VB line from a cell in spreadsheet

grahamiwa

New Member
Joined
Mar 14, 2011
Messages
30
I have a course booking spreadsheet that has a BeforeClose procedure that now works well.

I am trying to see if I can set it up as a template for other courses.

There are 3 variables in the code which I would like the code to get from named cells on the spreadsheet

I think that if I can get help to get the first one, the password, sorted then I should be able to figure out the rest.

The name of the cell for the password is Worksheet_Password.

The current line of code for the password protection is -

ActiveSheet.Unprotect Password:="SHES"


How can I make it go to the named Worksheet_Password cell for the password rather than having to write it in the code?

I am trying to make this workbook simple for the end user so that they do not have to alter the code
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi,

Do you mean like this:

ActiveSheet.Protect Password:=Range("A1").Value
ActiveSheet.UnProtect Password:=Range("A1").Value

Where A1 = your password ?
 
Upvote 0
Not quite James

The full code is

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Sheets("Course Booking").Select

ActiveSheet.Unprotect Password:="SHES"

Dim c As Range
For Each c In Range("C6:J110")
c.Locked = c.Value <> ""
Next c

ActiveSheet.Protect Password:="SHES", DrawingObjects:=True, Contents:=True, Scenarios:=True

ActiveWindow.ScrollWorkbookTabs Position:=x1First
Sheets("READ ME").Select

ActiveWorkbook.Save

strFileName = "
\\mlbdat02\shared\organization\T&D-All\ePTWfile://\\mlbdat02\shared\organization\T&D-All\ePTW\file://\\mlbdat02\shared\organization\T&D-All\ePTW\\ Course Scheduling\ePTW Schedule Master\Archive\Course Schedule Master" & Chr(32) & Format(Date, "d mmm yyyy") & ".xls"

Application.DisplayAlerts = False

ActiveWorkbook.SaveAs Filename:=strFileName, FileFormat:= _
xlNormal, Password:="SHES", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False

Application.DisplayAlerts = True

ActiveWorkbook.Close


End Sub


I would like to use this workbook as a template for other courses.

If I leave it as it is then the code will need to be edited for Password (currently SHES), File Name (Course Schedule Master) and probably file path.

What I want to do is to put these three variables in named cells in the spreadsheet and have the procedure refer to these cells for the actual values rather than putting them in the code. This way a non VB person can just put the required details in cells and the code will do the rest. I have highlighted in the above code the parts that need to be looked up in named cells
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

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