mark hansen
Well-known Member
- Joined
- Mar 6, 2006
- Messages
- 534
- Office Version
- 2016
- Platform
- Windows
I'm creating a template workbook that will have the ability for users to put in the password they were given to surface only the worksheets they are allowed to look at. The rest of the worksheets will be "very hidden". The worksheets will be password protected from editing so protection will be turned on.
In order for a user to set this up, I've come up code that looks at the password they enter to view the worksheet(s) that password is set up to show. So I have a grid with the 11 worksheets available with the view password, and the sheet protection password. (This allows the admin to set things up without digging into VBA)
When hiding and protecting the worksheets, I have a loop that sets the sheet to very hidden and protects the sheet with the password from the grid in the set up area.
The problem I'm having is finding the property of a worksheet the user normally cannot change. I thought it was the sheet number. But what I'm starting to discover, the order of the tabs seems to affect the sheetnumber (sn) in the code.
The reason I think this is, when I run the following loop, what ever worksheet I have first in order, this is the worksheet used with with "With Sheets(sn)" statement. When starting, the variable "sn" i=1 , and the next 2 .Range statements write text on the first sheet in the worksheet tab order, not on Sheet1. The code name is correct, but because the .Protect Password line uses the Password set aside for sheet 1, (starting at line 34) the sheet protecting password is wrong.
Why does the Sheets(sn) (when sn is equal "1") write on the worksheet tab to the left, in cells AJ34 and AJ35, rather than on Sheet1? (the .Range lines are temporary lines to see what's going on and what password is used to protect the sheet.)
I would have thought the With Sheets(sn) statements would work with the sheet numbers in number order.... Not the order they appear at the botton of Excel.
Thanks for any insight.
Mark
In order for a user to set this up, I've come up code that looks at the password they enter to view the worksheet(s) that password is set up to show. So I have a grid with the 11 worksheets available with the view password, and the sheet protection password. (This allows the admin to set things up without digging into VBA)
When hiding and protecting the worksheets, I have a loop that sets the sheet to very hidden and protects the sheet with the password from the grid in the set up area.
The problem I'm having is finding the property of a worksheet the user normally cannot change. I thought it was the sheet number. But what I'm starting to discover, the order of the tabs seems to affect the sheetnumber (sn) in the code.
The reason I think this is, when I run the following loop, what ever worksheet I have first in order, this is the worksheet used with with "With Sheets(sn)" statement. When starting, the variable "sn" i=1 , and the next 2 .Range statements write text on the first sheet in the worksheet tab order, not on Sheet1. The code name is correct, but because the .Protect Password line uses the Password set aside for sheet 1, (starting at line 34) the sheet protecting password is wrong.
Why does the Sheets(sn) (when sn is equal "1") write on the worksheet tab to the left, in cells AJ34 and AJ35, rather than on Sheet1? (the .Range lines are temporary lines to see what's going on and what password is used to protect the sheet.)
I would have thought the With Sheets(sn) statements would work with the sheet numbers in number order.... Not the order they appear at the botton of Excel.
Thanks for any insight.
Mark
Code:
Set PW = Sheet6
xRow = 33
For sn = 1 To 11
xRow = xRow + 1
With Sheets(sn)
.Range("AJ34").Value = Sheets(sn).CodeName
.Range("AJ35").Value = PW.Range("AF" & xRow).Value
.Protect Password:=PW.Range("AF" & xRow).Value
.Visible = xlSheetVeryHidden
' .Visible = True
End With
Sheet7.Visible = xlSheetVisible
Next sn