Adding unlocked rows into protected Excel sheet - excel

I am trying to make an Excel sheet where I show previous records to people and make them add new records using 'add row' functionality.
So I am making my excel sheet protected so they can not tamper with older records. And I am keeping one row unlocked where they can start adding new information.
I also checked the 'Allow adding new rows' option in protection dialog box.
The problem is when I add a new row in the protected sheet, all the cells in that row are locked and I am unable to make an input through it.
Is there a workaround for this? Thanks.

Simply add row just below unlocked row to avoid the issue.
Using VBA
You can explicitly specify the locked=false for new rows.
Try below sample code.
Sub test()
ActiveSheet.Unprotect "test"
Rows(5).Insert
Rows(5).Locked = False
ActiveSheet.Protect "test"
End Sub

The easy workaround without using macros is as follows:
You need two unprotected rows
Hide the first one
Protect the sheet and allow insertion/deletion of rows (not formatting.. they could unhide the row)
People then can add rows via HOME -> Insert -> Insert Sheet Rows or delete them in the same way.
(people can't select the whole rows because of possible locked cells on the right and press [CTRL]+[+])
DONE!

Related

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.

Deleting rows on protected sheet

I have a worksheet that is protected; all cells are locked besides those that are intended to be edited by the user. I would like the user to be able to delete rows, so I enabled 'delete rows' when protecting my sheet. As expected I receive the error message "You are trying to delete a row that contains a locked cell..." I understand why this is happening as all cells outside the area I expect user input are locked.
I don't want to unlock all other cells as I don't want users to enter data in them.
I have researched the ability unprotect the sheet on a 'delete row' event but wasn't able to find such an event. I guess I can add a button next to each row that unprotects the sheet deletes the row and reprotects the sheet but was hoping for a more elegant/easier solution.
How can I delete a row in a protected sheet when there are locked cells in the columns to the right of my table?
You can have a button which calls a VBA function which does the deleting. And use
Protect UserInterfaceOnly:=True
when protecting the sheet.This way you'll be able to delete rows and perform other actions via VBA on a protected sheet.
You can normally achieve this via the following code:
Public Sub ProtectMySheet()
Sheet1.Protect UserInterfaceOnly:=True
End Sub
However, I've discovered this to be hit or miss sometimes. So I sometimes do this
Public Sub EditStuffInProtectedSheet()
With Sheet1
.Unprotect MYPASSWORD ' You can store your password in a safe location, or make it a constant (if you're okay with your End Users knowing it)
' ***Do Stuff***
.Protect MYPASSWORD
End With
End Sub

VBA How to lock only certain columns in a sheet based on date [duplicate]

I have an Excel worksheet that acts like an application, with form control buttons allowing users to 'navigate' through records. First, Previous, Next & Last cycle appropriately through one of the worksheets records, displaying the values in my 'form' worksheet.
When users are not in Edit or Add Mode, I would like to lock the cells to prevent users from modifying contents.
I tried Range("A1:O24").Locked = True, but I am still able to type new values into the cells.
Anyone know how to accomplish this? I need my vba code to be able to assign new values to the cells as users 'navigate', but to prevent users from entering new values unless in Add or Edit mode.
I believe the reason for this is that you need to protect a worksheet before cells actually become locked. All cells are formatted as locked as a default so what you really want to do is set the range that you don't want locked to Range().Locked = False and then set the worksheet to protected.
In the case that you want all cells locked all you have to do is set the worksheet to protected
Search for your condition whether user was not in Edit or Add mode and then locking your range and finally protect your worksheet.
Let's say for example in one case, if you want to locked cells from range A1 to I50 then below is the code:
Worksheets("Enter your sheet name").Range("A1:I50").Locked = True
ActiveSheet.Protect Password:="Enter your Password"
In another case if you already have a protected sheet then follow below code:
ActiveSheet.Unprotect Password:="Enter your Password"
Worksheets("Enter your sheet name").Range("A1:I50").Locked = True
ActiveSheet.Protect Password:="Enter your Password"

Paste special with autofilter and unfilter after autofilter and paste is complete

I am using the solution from this post to autofilter a table and paste the selected columns to another sheet.
It is doing exactly what I want it to do with one exception. I am copying from a table that uses filtered drop down menus. As a result, the following message pops up while copying: The name already exists
The solution offered in this blog is not an option as the source table is dependent on the named values in it.
Is there an additional statement I can add to the below code that will make it paste special?
copyRange4.SpecialCells(xlCellTypeVisible).Copy tgt.Range("E10")
If not, can something be added to select "Yes" without the pop up appearing at all?
Finally, after it pastes to my target, can an additional line be added to unfilter the source?
Disable Alert to by-pass the alert,
Application.DisplayAlerts = False
copyRange4.SpecialCells(xlCellTypeVisible).Copy tgt.Range("E10")
Application.DisplayAlerts = True
To unfilter,
If (Range).AutoFilterMode Then
(Range).ShowAllData
End If
Where (Range) is your range with filter.

Remove error indicating unlocked cells are protected with VBA

I have a template sheet that is copied through VBA into a new sheet.
The new sheet is added by clicking on a button that makes a userform pop up, and the information in the userform is transferred to the new sheet.
This new sheet contains locked headline cells as well as unlocked empty cells for the user to put information into. The sheet is protected but allows users to select locked and unlocked cells.
Everything worked for some months until the other day. When a new sheet is created and the user tries to fill in the unlocked cells, Excel responds as if they were locked!
The following information pops up:
"The cell or chart you're trying to change is on a protected sheet. To make changes, click Unprotect Sheet in the Review tab (you might need a password)."
This error disappears after the user does any of the following
1) double click in any empty cell (instead of just selecting it) and put in information this way and then click enter, then all cells behave normally.
2) activate any other worksheet and then return to this sheet
3) choose Format>Unprotect Sheet (this does not unprotect the sheet but it removes the error. If the user wants to unprotect the sheet then they have to click Format>Unprotect Sheet twice, i.e. first to remove this weird error and then to unprotect it).
I am trying, with VBA, to remove this error by doing any of 1, 2 or 3 above.
Any ideas how to solve this?
An additional problem is it is not possible to scroll with the mouse in the sheet while having the problem.
I tried the following code:
ActiveSheet.EnableOutlining = True
ActiveSheet.Protect Contents:=True, userInterfaceOnly:=True
After some googling I managed to solve this by doing the following:
Create a Module in VBA and add the following code:
Public Sub RefreshActiveCellSelection()
ActiveCell.Select
End Sub
Whenever you perform a Workbook.Activate or Worksheet.Activate, immediately call
Application.OnTime Now, "RefreshActiveCellSelection"
Another solution was to uninstall KB3085502
I found the answer here: http://answers.microsoft.com/en-us/office/forum/office_2013_release-office_install/microsoft-update-excel-2013-kb3085502-32-bit/0cc7d498-92cb-4478-9554-77cff286c847

Resources