SOLVED

Lock worksheet cell

%3CLINGO-SUB%20id%3D%22lingo-sub-1926771%22%20slang%3D%22en-US%22%3ELock%20worksheet%20cell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1926771%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%20I%20have%20locked%20cells%20in%20my%20worksheet%20and%20all%20works%20well.%20But%20i%20have%20a%20macro%2Fvba%20code%20that%20fills%20a%20cost%20value%20in%20certain%20cells.%20It%20all%20works%20perfectly%20if%20the%20cells%20is%20not%20locked%20as%20soon%20as%20i%20lock%20these%20cells%20the%20cost%20value%20do%20not%20appear%20in%20these%20cells%2C%20why%20and%20what%20must%20i%20do%3F%3F%3F%3F%3F%3F%3F%3F%3F%3C%2FP%3E%3CP%3EThank%20You%3C%2FP%3E%3CP%3ERegards%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1926771%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1926918%22%20slang%3D%22en-US%22%3ERe%3A%20Lock%20worksheet%20cell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1926918%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F826237%22%20target%3D%22_blank%22%3E%40kobus1305%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20unprotect%20the%20worksheet%20at%20the%20beginning%20of%20your%20macro%2C%20and%20protect%20it%20again%20at%20the%20end%20of%20the%20macro.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1926925%22%20slang%3D%22de-DE%22%3ESubject%3A%20Lock%20worksheet%20cell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1926925%22%20slang%3D%22de-DE%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F826237%22%20target%3D%22_blank%22%3E%40kobus1305%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20the%20permission%20of%20all%20involved%2C%20I%20would%20like%20to%20add%20these%20examples%20as%20a%20very%20good%20suggestion%20from%20Mr.%20Hans%20Vogelaar.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3ESheet%20protection%20macro%20without%20password%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EThe%20following%20macros%20are%20used%20to%20set%20and%20remove%20sheet%20protection%20for%20Sheet1%20without%20a%20password.%3C%2FP%3E%3CP%3ESub%20sheet%20protection_on%20()%3C%2FP%3E%3CP%3ESheets%20(%22Table1%22).%20Protect%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20sheet%20protection%20off%20()%3C%2FP%3E%3CP%3ESheets%20(%22Table1%22).%20Unprotect%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E--------------------------------------------------%3C%2FP%3E%3CP%3E%3CSTRONG%3EMacro%20for%20sheet%20protection%20with%20password%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3EThe%20sheet%20protection%20can%20also%20be%20set%20with%20a%20password.%20In%20the%20following%20example%2C%20the%20password%20is%20%22myPassword%22.%3C%2FP%3E%3CP%3ESub%20Blattschutz_on_with_Passwort%20()%3C%2FP%3E%3CP%3ESheets%20(%22Table1%22).%20Protect%20Password%3A%20%3D%20%22myPassword%22%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20Sheetprotect_off_with_Passwort%20()%3C%2FP%3E%3CP%3ESheets%20(%22Table1%22).%20Unprotect%20Password%3A%20%3D%20%22myPassword%22%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E------------------------------------------------------%3C%2FP%3E%3CP%3E%3CSTRONG%3ESheet%20protection%20for%20all%20tables%20in%20the%20workbook%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EA%20for%20loop%20is%20used%20to%20activate%20or%20deactivate%20sheet%20protection%20for%20all%20tables%20in%20a%20workbook.%20The%20following%20example%20uses%20sheet%20protection%20without%20a%20password.%3C%2FP%3E%3CP%3ESub%20Sheetprotect_on_all_Sheets%20()%3C%2FP%3E%3CP%3EFor%20Each%20sheet%20In%20ActiveWorkbook.Worksheets%3C%2FP%3E%3CP%3ESheet.Protect%3C%2FP%3E%3CP%3ENext%20sheet%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESub%20Sheetprotect_off_all_Sheets%20()%3C%2FP%3E%3CP%3EFor%20Each%20sheet%20In%20ActiveWorkbook.Worksheets%3C%2FP%3E%3CP%3ESheet.Unprotect%3C%2FP%3E%3CP%3ENext%20sheet%3C%2FP%3E%3CP%3EEnd%20Sub%3C%2FP%3E%3CP%3E---------------------------------------------------%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20understanding%20and%20patience%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWish%20all%20a%20nice%20day.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENikolino%3C%2FP%3E%3CP%3EI%20know%20I%20don't%20know%20anything%20(Socrates)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1927089%22%20slang%3D%22en-US%22%3EBetreff%3A%20Lock%20worksheet%20cell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1927089%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F722750%22%20target%3D%22_blank%22%3E%40Nikolino%3C%2FA%3E%26nbsp%3BThank%20You%20so%20much.%20The%20code%20that%20says%26nbsp%3B%3CSTRONG%3ESheet%20protection%20for%20all%20tables%20in%20the%20workbook%26nbsp%3B%3C%2FSTRONG%3Eis%20the%20coding%20that%20seems%20the%20most%20simple%20and%20easy.%20I%20am%20sending%20you%20the%20workbook%20please%20show%20me%20to%20implement%20that%20coding%2C%20You%20will%20see%20on%20the%20workbook%20Product%20Description%20is%20a%20drop%20down%20list%20to%20select%20from%20and%20then%20the%20Unit%20Price%20get%20populated%20with%20the%20selected%20product%20from%20Unit%20Price%20worksheet.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1927391%22%20slang%3D%22de-DE%22%3ERE%3A%20Lock%20worksheet%20cell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1927391%22%20slang%3D%22de-DE%22%3EI%20was%20happy%20to%20help%20you.%20I%20wish%20you%20continued%20success%20with%20Excel%20(the%20coolest%20invention%20since%20chocola%20...%20Uh...%20Microsoft!%20%3A-)))%20And...%20Please%20keep%20asking%20here%20-%20I%20just%20taught%20myself%20Excel%20with%20the%20help%20of%20this%20forum...%20almost%20%3A))%20Nikolino%20I%20know%20I%20don't%20know%20anything%20(Socrates)%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1927516%22%20slang%3D%22en-US%22%3ERE%3A%20Lock%20worksheet%20cell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1927516%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F722750%22%20target%3D%22_blank%22%3E%40Nikolino%3C%2FA%3E%26nbsp%3BYou%20never%20attached%20the%20altered%20worksheet%3F%3F%3C%2FP%3E%3CP%3EThank%20You%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

Hi, I have locked cells in my worksheet and all works well. But i have a macro/vba code that fills a cost value in certain cells. It all works perfectly if the cells is not locked as soon as i lock these cells the cost value do not appear in these cells, why and what must i do?????????

Thank You

Regards

12 Replies

@kobus1305 

You can unprotect the worksheet at the beginning of your macro, and protect it again at the end of the macro.

best response confirmed by kobus1305 (Contributor)
Solution

@kobus1305 

With the permission of all involved, I would like to add these examples as a very good suggestion from Mr. Hans Vogelaar.

 

Sheet protection macro without password

The following macros are used to set and remove sheet protection for Sheet1 without a password.

Sub sheet protection_on ()

    Sheets ("Table1"). Protect

End Sub

 

Sub sheet protection off ()

    Sheets ("Table1"). Unprotect

End Sub

--------------------------------------------------

Macro for sheet protection with password

The sheet protection can also be set with a password. In the following example, the password is "myPassword".

Sub Blattschutz_on_with_Passwort ()

    Sheets ("Table1"). Protect Password: = "myPassword"

End Sub

 

Sub Sheetprotect_off_with_Passwort ()

    Sheets ("Table1"). Unprotect Password: = "myPassword"

End Sub

------------------------------------------------------

Sheet protection for all tables in the workbook

 

A for loop is used to activate or deactivate sheet protection for all tables in a workbook. The following example uses sheet protection without a password.

Sub Sheetprotect_on_all_Sheets ()

    For Each sheet In ActiveWorkbook.Worksheets

        sheet.Protect

    Next sheet

End Sub

 

Sub Sheetprotect_off_all_Sheets ()

    For Each sheet In ActiveWorkbook.Worksheets

        sheet.Unprotect

    Next sheet

End Sub

---------------------------------------------------

Thank you for your understanding and patience

 

Wish all a nice day.

 

Nikolino

I know I don't know anything (Socrates)

 

 

@NikolinoDE Thank You so much. The code that says Sheet protection for all tables in the workbook is the coding that seems the most simple and easy. I am sending you the workbook please show me to implement that coding, You will see on the workbook Product Description is a drop down list to select from and then the Unit Price get populated with the selected product from Unit Price worksheet.

I was happy to help you. I wish you continued success with Excel (the coolest invention since chocola ... uh ... Microsoft! :-))) And… Please keep asking here - I just taught myself Excel with the help of this forum… nearly :)) Nikolino I know I don't know anything (Socrates)

@NikolinoDE You never attached the altered worksheet??

Thank You

@kobus1305 

You have a dropdown menu in your inserted file,

if you lock this as a whole workbook, it will also be locked,

which I think cannot meet your requirements.

Enclosed your file was blocked in January with leaf protection so that the dropdown lists remain free.

Look at January

 

Thank you for your understanding and patience

 

Wish you a nice day.

Nikolino

@NikolinoDE Hi SORRY i was under the impression i have disabled the worksheet protection. I am sending the workbook to you again.

Thank You

Regards

@kobus1305 

Please try it out, it should work.

I actually only built the protection into your macro in the workbook.

 

at the beginning of the macro

Me.Unprotect ("1234")

 

and at the end of the macro

Me.Protect ("1234")

 

With my Office versions 2013 and 2016 it works without problems.

 

Thank you for your patience and time.

 

Nikolino

I know I don't know anything (Socrates)

@NikolinoDE Hi I do not know what it is but it do not work. I have Windows 7 Pro and Office 2013 Pro. Here is the code

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'
'Insert price for selected product
'
If Target.CountLarge > 1 Then Exit Sub
Dim r As Long
Dim c As Long
'
Me.Unprotect ("Largo123")
r = Target.Row
c = Target.Column
On Error GoTo Skip
If VBA.Trim(LCase(Sh.Cells(2, c).Value)) = "product description" Then
If Target <> "" Then
Application.EnableEvents = False
With Target.Offset(0, -2)
.FormulaR1C1 = "=VLOOKUP(RC[2],'Product List'!R3C1:R20C3,3,FALSE)"
.Value = .Value
End With
Else
Target.Offset(0, -2) = 0
End If
End If
'
Skip:
Application.EnableEvents = True
Me.Protect ("Largo123")
End Sub

I have checked the execution line by line if the worksheet protection is on it bombs out at the vlookup line but if you disable the worksheet protection it works perfectly. Is there any setting or something that i have to change at my end?

Regards

Thank You

@kobus1305 

You automatically renew data in certain areas / columns / cells, you have to unlock this area / column / cells with the cell formatting before you lock the workbook.

Otherwise these columns / cells or computation cannot change with the content because they are locked.

Here are the instructions:

Lock or unlock specific areas of a protected worksheet

https://support.microsoft.com/en-us/office/lock-or-unlock-specific-areas-of-a-protected-worksheet-75...

 

The file I sent you earlier, didn't it work?

 

Thank you for your patience and time.

 

Nikolino

I know I don't know anything (Socrates)

Not even 1 'like' with Excel.

@kobus1305 

Please see the month of January.

Is that what you need If so, copy the January VBA code into each individual sheet in all worksheets.

 

Hope this will help

 

Thank you for your patience and understanding

 

Nikolino