Unprotect and change style with vba - excel

Sheet1.Unprotect
Sheet1.Cells(13, "E").Locked = False
Sheet1.Range("E13").Style.Name = "Normal"
Sheet1.Protect
Produces:
The cell or chart you're trying to change is on a protected sheet.
To make changes, click on Unprotect Sheet in the Review tab (...)
On the other hand:
Sheet1.Unprotect
Sheet1.Cells(13, "E").Locked = False
Sheet1.Protect
runs instead properly.

Had the same problem with the, 'The cell or chart you're trying to change is on a protected sheet. To make changes, click on Unprotect Sheet in the Review tab (...)', error message coming up.
Realized I had protection on an another worksheet in the workbook which had to be turned off before I could modify the Normal style. Makes sense if you think about it!
Once all protection was turned off I can modify the Normal Style.
Rogerc

Related

Edit Objects are not working in protected workbook

I have an excel document that is protected. I have a macro running to allow for expanding and collapsing rows in 1 of the sheets that looks like this:
Private Sub Workbook_Open()
With Worksheets("Part 1")
.EnableOutlining = True
.Protect UserInterfaceOnly:=True
End With
End Sub
This was working great, however now I would like to allow for "drawing" to have the text in that spreadsheet to be highlighted. I have gone into the "protect sheet" function to allow for "edit objects", I protect the sheet, save and exit. When I reopen the document it does not allow for me to use the "draw" function. And when I unprotect the sheet, the "edit objects" is selected, but the draw buttons do not work until I "unprotect" and then "protect" it again.
This is only happening in the sheet that has the macro running, thinking this might be a macro code. Any ideas?
As mentioned above, I have tried to unlock and relock the spreadsheet, however the edit objects function does not save upon next reopen. I am hoping to get assistance on the macro as I feel it is creating the error.

Hide or unhide Excel sheet but formulas should work

I have used this method before but after six months I cannot.
I created data entry fields through which I move data from Sheet1 to Sheet2 using a button.
I do not want users to see Sheet2 so that they cannot change anything. I was using the formulas below.
Sheets("Data Sheet").Visible = True
Sheets("Data Sheet").Visible = False
After hiding the sheet I protect them with the password. The main advantage of code is that I can still post the data from Sheet1 to Sheet2 after hiding.
Upon research I found one more formula to hide the sheet but after hiding sheet2 posting via Sheet1 to Sheet2 (which is hidden) gives an error.
ThisWorkbook.Worksheets(Array("Data Sheet")).Visible = xlSheetHidden
How can I hide Sheet2 and continue posting the entries via Sheet1.
You can manipulate data on a hidden sheet, no need to unhide it.
Unless you are trying to select cells, but you shouldn't have to do that.
You do however need to unlock a sheet before editing it if the cells are protected. You can do this as part of the process.
Sheets("Data Sheet").Unprotect Password:="pass"
----
whatever code you have
----
Sheets("Data Sheet").Protect Password:="pass"
You do might want to have some error handling in there however, since if the code throws an error, the sheet will be left unlocked.
a) The statements Sheets("Data Sheet").Visible = False and ThisWorkbook.Worksheets(Array("Data Sheet")).Visible = xlSheetHidden do basically the same. However, you will need to understand why it is important to qualify the Sheets: ThisWorkbook.Worksheets will access the worksheets from the workbook where the code lives in, just writing Sheets (or Worksheets) will access the sheets of the ActiveWorkbook - and that may be a different workbook. The Array("Data Sheet")-part is unnecessary in your case (you can pass an array of sheet names to hide more than a sheet at once). Setting visibility to xlSheetHidden or to False is identical.
b) Hiding a sheet and protecting a sheet are two different, independent things. You can protect a sheet but not hide it, you can hide a sheet but let it unprotected.
c) The main idea of protecting a sheet is to allow user input only at specific cells. The user can change the content of the sheet, but only to cells which are not formatted as "Locked".
d) If you protect a sheet via Excel (no matter if hidden or not) and want to modify something via code, you will need to unprotect it (and protect it again after the code is done). However, when protecting the sheet via code, you can specify that you want to allow the code to make modifications by setting the UserInterfaceOnly-parameter:
Thisworkbook.Sheets("Data Sheet").Protect Password = "IWontTellYou", UserInterfaceOnly:=True
e) If you never want to show the sheet, set the visibility not to hidden, but to veryHidden. With that, the sheet cannot be made visible by the use from within Excel: It will not be listed under "Unhide..." - in that case there is not need to protect it.
Thisworkbook.Sheets("Data Sheet").Visible = xlSheetVeryHidden
(Note that in that case you can make the sheet visible again only via code, but a one-liner in the immediate window is sufficient)
You will have to write a code to first unprotect sheet2, then paste the data and protect sheet2 again.

Is it possible to always stay in activesheet when hiding another?

I have created a dynamic workbook where i put and pull data to more sheets containing data from other sources. I have a "Temp" sheet where code updates som data, this sheet is hidden until the macro is run, to put and pull data, i make it visible and when finished i hide it again. This works (almost) perfect.
The problem occurs when i hide it, then my last Sheet i selected automatically? My code is totally without any .select or .active (Still learning) because it is important that code always return to the sheet from where the code is run as this changes name everyday.
I can't seem to find anything that solves my issue, hope somebody can help:
I tried the following, as you can see in the following code:
Sheets("Temp").Cells.ClearContents
Sheets("Temp").Visible = False
'Cleans filters
ActiveSheet.ShowAllData
Application.Goto Range("A4")
Application.ScreenUpdating = True
I want to always stay in my activesheet (example: ("16") which is the actual date, there will be one sheet for everyday etc. ("17"), ("18") and so on.)
You can use With to immediately reactivate the sheet, like so:
With ActiveSheet
OtherSheet.Visible = xlSheetVisible
.Activate 'Immediately reactivate the ActiveSheet
End With
(You may want to toggle Application.ScreenUpdating so that you do not see so much as a flicker of the other sheet)
However, why do you need to show the sheet to copy data from it? If you are not using Select (which you say you are already sorted on), then you should be able to do most things with a Hidden sheet, such as HiddenSheet.Range("A1:B3").Copy
(A "Very Hidden" sheet, on the other hand, has a couple of restrictions - for example, you cannot delete a Very Hidden sheet.)

Excel thinking cells are locked when they aren't

I have quite a complicated workbook which i have very heavily locked down so it cant lose its structural integrity when being used by the user. It works like a dream in Excel 2003, 2007 and 2010 however when i use it in 2013 if i try and type in to an unprotected cell excel thinks that its protected, i can then unprotected the sheet manually and it still wont let me type in to the cell im then mysteriously allowed to unprotected the worksheet AGAIN and then it allows me to enter in to the cell and also get in to the cell formatting to demonstrate that the cell is definitely not locked. Its then made even more complicated when i then navigate to another protected sheet and i then find it unprotected.....
I have messed about with the sheet more and more and the problem appears to be when my macro moves me from one sheet to the next it believes its still on the previous sheet.
For example on the front sheet i have cell K16 as named cell called "ChangePW"
I then run my macro to select another sheet (shown below)
Application.ScreenUpdating = False
Sheets("Income&Expenditure").Select
Worksheets("Income&Expenditure").Unprotect Password:="*****"
Range("B3").Value = "OSC"
Worksheets("Income&Expenditure").Protect Password:="*****"
Range("B2").Select
Application.ScreenUpdating = True
I then proceed to enter some data in to the the Income&Expenditure tab but i get a warning that the sheet is protected even though i have selected an unprotected cell. I then click on cell K16 on the Income&Expenditure sheet and it still thinks its called "ChangePW". As a result of this its applying the protection layout of the front sheet on the Income&Expenditure sheet. Even when i click on the Income&Expenditure tab at the bottom and click "Show Code" its showing me the code for the Front Sheet!!
All of the vba works perfectly in 2007 and 2010 so im absolutely stumped at the moment.
I have been navigating forums for the last 2 weeks trying to find an explanation with no successes, has anyone else experienced anything like this or know of a cause? i reiterate that it works perfectly in all other versions of Excel....
Thanks
I've been solving the same behaviour. The bug is in opening files from a modal UserForm. The solution is to open a UserForm as modeless.
Either calling:
MyForm.Show False
or adding Layout event procedure to a UserForm:
Private Sub UserForm_Layout()
Static fSetModal As Boolean
If fSetModal = False Then
fSetModal = True
Me.Hide
Me.Show 1
End If
End Sub
Read: http://msdn.microsoft.com/en-us/library/office/dn251093.aspx

Protected Sheet Warning On Any Edit

Hoping to get some help with Excel 2010.
I have three visible sheets. On Workbook Open, vba password protects each of the three sheets.
Now, whenever I make a change to any unlocked cell in any sheet, I get 4 protected sheet warning pop-ups. The one that says that the cells are locked and you have to unprotect to edit? That's all well and good except that I am not editing locked cells. I am editing unlocked cells in other sheets!
Has anyone had this experience? I have played around and two of them can be attributed to each of two sheets. That is to say, when I only protect the first sheet, I get no pop-ups, when I protect only the second, I get 2 and when I protect only the third, I get 2.
Taking out formulas hasn't made a difference.
Here is the code for the locking:
For Each wSheet In Worksheets
wSheet.Unprotect Password:="JMP_DST_Lock"
If wSheet.Visible = True Then
wSheet.Protect Password:="JMP_DST_Lock", UserInterFaceOnly:=True
wSheet.EnableOutlining = True
End If
Next wSheet
ThisWorkbook.Protect Password:="JMP_DST_Lock"
Thank you very much for any help.
EDIT:
Turns out the comboboxes are the reason for the error. If the linked cells are locked, anytime the sheet calculates (any change when on automatic calculation) causes the warnings. Now, I have code that unprotects the sheets on each combobox GotFocus, but aside from that, these are cropping up.
Is there a middle ground? A way to keep the linked cells locked without these warnings popping up? A way to make sure the comboboxes are hitting the linked cells except on selection?
Thank you!
Your logic seems wrong. You're unprotecting the sheet, then protecting visible sheets, and then protecting the entire workbook (in the last code line).
Try something like this instead (untested):
For Each wSheet In Worksheets
If wSheet.Visible = True Then
wSheet.Protect Password:="JMP_DST_Lock", UserInterFaceOnly:=True
wSheet.EnableOutlining = True
End If
Next wSheet
If the workbook is saved with the sheets in protected state, change it to something like this instead:
For Each wSheet In Worksheets
If wSheet.Visible = False Then
wSheet.UnProtect Password:="JMP_DST_Lock"
End If
Next wSheet

Resources