VBA code for protecing range of cells (user intervention) - vba

I want VBA code for protecting a range of cells(manually selected by user) in excel and rest of the cells will be locked. Ideally, I don't need to restrict the code for a specific range, it should allow a user to select a range of cell and lock those cells.
Please help me.

In Change of a the worksheet, lock the cells you selected and then, protect the worksheet.
something like :
with thisworkbook.worksheets("the name of the worksheet")
.unprotect
Target.locked=true
.protect
end with

Related

Activate range of an inactive workbook

In the following code:
Dim FirstSourceCell As Range
Set FirstSourceCell = Workbooks("Source.xlsx").Worksheets ("Settlements").Range("M9")
FirstSourceCell.Activate
why does the FirstSourceCell.Activate works only when the Source.xlsx workbook is activated?
You can only activate a cell in the currently active worksheet. So if the workbook isn't active, the worksheet can't be active, therefore you will get an error if you try to activate a cell on that inactive worksheet.
But there are very few cases where a cell needs to be activated. About the only real case is where you want to return control to the user with a specific cell selected. In that case, it is better to use Application.GoTo FirstSourceCell.

Excel: Copy Dynamic Range from one worksheet to another

I am trying to copy a dynamic range (B12:Lxx)from one worksheet to another. I need the range being copied to stop at the first empty row (there is additional data further down the sheet which I don't want copied).
I am a very basic VBA user so if you could explicitly set out your instructions that would be handy.
Source: Worksheet "MyKPIs" with the dynamic range B12:Lxx (column L is set, row numbers are variable BUT must end at the first empty row)
Target: Worksheet "Month Template", cell B5
Trigger would be a command button
I have trawled through other articles but have failed to find anything that I could use.
Thanks,
Hayley
this will work. insert a command button on your worksheet. double click the button. paste in this code between sub and end sub.
Worksheets("MyKPIs").Range("b12").CurrentRegion.Copy Worksheets("Month Template").Range("b5")
it should look like this when you are through. then go to your worksheet on developer tab toggle off design mode then click the button.
Private Sub CommandButton1_Click()
Worksheets("MyKPIs").Range("b12").CurrentRegion.Copy Worksheets("Month Template").Range("b5")
End Sub
for those inexperienced with currentregion please look at the 2 samples below that have blank cells but the region is selected and you can easily see the beginning and ending points in the range and how an entire blank row or column forms the range.
Place a command button from the Forms toolbar on your "MyKPIs" sheet.
Then add this code:
Sub Button1_Click()
Dim myrange
Set myrange = Sheets("MyKPIs").Range("B12:L12")
myrange.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Worksheets("Month Template").Range("B5")
End Sub
Considering all cells in column B have data, this will copy all the cells in the range. It will stop at the first empty cell in column B. This should help you to start.

Paste after unprotecting worksheet not working

I have two sheets "Request" and "Changes" both protected by same password. Password has been stored as a constant in another module. I have tested this code when "Changes" sheet in unprotected and it works, however for some reason, if I have to unprotect the sheet before pasting it won't paste.
I am simplifying trying to keep a record of what was deleted in the workbook by placing the info in a protected sheet "Changes" that is hidden.
Sub PODelete()
Dim rng As Range, ws As Worksheet, ws1 As Worksheet, lr As Integer
Set ws = Sheets("Request")
Set ws1 = Sheets("Changes")
lr = ws1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row 'Last non-blank row for column A
On Error Resume Next
Set rng = Application.InputBox("Select a range", Type:=8)
If Not rng Is Nothing Then
' Checks to see if column A is selected
If rng.Cells.Column = 1 Then
' Checks to see that only 1 cell is selected
If rng.Cells.Count > 1 Then
MsgBox "Select only one P.O. Number to remove"
Else
' Select entire row
rng.EntireRow.Copy
' Activate sheet and pastes values
Sheets("Changes").Activate
ws1.Unprotect Password:=worksheetpassword
Cells(lr, 1).EntireRow.Select
Selection.PasteSpecial Paste:=xlPasteValues
ws1.Protect Password:=worksheetpassword, DrawingObjects:=True, Contents:=True, Scenarios:=True
' Deletes range
ws.Activate
ws.Unprotect Password:=worksheetpassword
rng.EntireRow.Delete
ws.Protect Password:=worksheetpassword, DrawingObjects:=True, Contents:=True, Scenarios:=True
End If
Else
MsgBox "Select P.O. Number to remove"
End If
Else
Exit Sub
End If
End Sub
Once again, if I run this macro with the "Changes" sheet unlocked it will work.
The real answer to this is actually not to unprotect the Worksheet at all. Protect it with a different password that isn't stored in the file anywhere, but set the parameter UserInterfaceOnly:=True. This will allow the macro to make changes to the worksheet, and even if the user find the hidden sheet they won't be able to find the password.
Speaking of hiding it, you should really hide it. As far as I know this is only possible through VBA, but you can set the Worksheet to xlSheetVeryHidden, either through the VBE...
...or in code:
Worksheets("Changes").Visible = xlSheetVeryHidden
Then password protect the VBA project with the same password as the Worksheet, and nobody is going to find it but you unless they know the name of the sheet and manually type it into a cell formula:
=Changes!A1
If you want to avoid that possibility, just name the Worksheet something non-obvious or even total gibberish. The likelihood of an unsuspecting user typing this into a cell...
=kusidkeshlkiehas!A1
...is virtually zero.
I have found that occasionally doing an Unprotect on the active sheet will change the active sheet to a different sheet. I haven’t figured out under what circumstances this occurs or why the particular sheet that ends up selected gets selected. Could this be your problem? I solved this problem by having one routine that is called to do all Unprotects. In that routine I save the active sheet name before doing the Unprotect and then re-select the saved sheet. I haven't seen this problem with Protecting a sheet but just to be safeI have done the same thing with a Protect routine.
I tried to copy a range from one work book(selection.copy only once and keeping the workbook open) and through "for loop", to many other work books(ActiveSheet.Unprotect "password", ActiveSheet.Paste).
This works fine during 1st trial but fails during subsequent trials on the same file with a message "Error 1004 Paste Method of Worksheet Failed" suggested by Mr Darrel Dixon and various other solutions of "UserInterfaceOnly:=True" etc but the error is repeating. The sheet appears UNPROTECTED at that stage and copy from source workbook and paste into destination workbook MANUALLY works fine but through VBA macro, it fails.
Finally, I tried repeating "selection.copy" from source workbook after "ActiveSheet.Unprotect" on each destination workbook/each iteration of for loop.
Even though this appears to be inefficient code, this alone solved the problem of PASTE failure after UNPROTECTING a worksheet.

Protect cells in excel but should be able to copy them

If I use this code to protect a worksheet, how can I make it so that the user still can copy the cells? And can you specify certain cells that should NOT be protected, or at least the user should be able to edit them?
Worksheets("EKONOMI").Protect UserInterfaceOnly:=True
You can define if user is allowed to select cells in protected sheet by using
Worksheets("EKONOMI").EnableSelection = xlNoRestrictions 'worksheet has to be protected for this to take effect
To make user able to edit certain cells you have to unlock the cells before the protection. For instance you can use the following to make Range C3 as unlocked cell
ActiveSheet.Range("C3").Select
Selection.Locked = False
Sheets("EKONOMI").Activate
ActiveSheet.Unprotect Password:="123"
'ActiveSheet.Protection.AllowEditRanges(1).Delete
ActiveSheet.Protection.AllowEditRanges.Add Title:="Range1", Range:=Range("A1:A10")
ActiveSheet.Protect Password:="123"
Please find the above code to protect sheet by allowing user to edit patricular range of cells.
ActiveSheet.Protection.AllowEditRanges.Add Title:="Range1", Range:=Range("A1:A10")
Mention the range name and range size for the user to edit.

VBA seems to disappear after I protect a range of cells

Hi I can't seem to get my head around this problem:
After I lock a range of cells A1:U1 (and all other cells are unlocked) and I then protect the worksheet - the VBA in the worksheet doesn't appear to work anymore - is there any way around this??
Thanks,
Kieran
If the VBA changes cells that you have locked then you need to update the VBA to unlock the worksheet before running the rest of the code. Then complete the Sub by locking the worksheet.
'Add to beginning of the Sub
Sheets("worksheet_name").Unprotect Password:="password"
'Add to end of the Sub
Sheets("worksheet_name").Protect Password:="password"