Home

Default Cell value & Protecting a worksheet that uses macros

%3CLINGO-SUB%20id%3D%22lingo-sub-401501%22%20slang%3D%22en-US%22%3EDefault%20Cell%20value%20%26amp%3B%20Protecting%20a%20worksheet%20that%20uses%20macros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-401501%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20back%20again%20working%20on%20this%20meal%20planner.%20Thanks%20to%20the%20assistance%20of%20some%20users%20here%20I%20have%20been%20able%20to%20get%20it%20up%20and%20running%20for%20the%20most%20part.%20However%2C%20I%20am%20stuck%20on%20two%20things.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20like%20a%20range%20of%20cells%20to%20have%20a%20default%20value%20so%20that%20when%20data%20is%20deleted%20from%20those%20cells%20the%20sheet%20will%20reset%20the%20default%20to%201%20instead%20of%20becoming%20blank.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F106045iB7876932303CCB80%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22default%20value.png%22%20title%3D%22default%20value.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EI%20would%20like%20all%20of%20the%20cells%20with%20the%20value%20of%20one%20in%20column%20C%20to%20reset%20to%20a%20default%20value%20when%20erased%20(minus%20the%20ones%20that%20say%20grams).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20also%20like%20to%20be%20able%20to%20protect%20this%20workbook%20from%20editing%20in%20all%20cells%20on%20sheet%201%20bar%20columns%20A%2C%20B%20and%26nbsp%3BK2%2C%20L2%2C%20N2%20and%20M2%20as%20these%20cells%20will%20be%20used%20to%20input%20data%20from%20the%20user.%20The%20problem%20is%20that%20I%20have%20a%20Macro%20running%20to%20clear%20columns%20A%20and%20B%20so%20using%20protected%20mode%20seems%20to%20be%20out%20of%20the%20question.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20Ideas%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-401501%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-421019%22%20slang%3D%22en-US%22%3ERe%3A%20Default%20Cell%20value%20%26amp%3B%20Protecting%20a%20worksheet%20that%20uses%20macros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-421019%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%20For%20the%20default%20value%20you%20could%20do%20this%20(Needs%20to%20be%20on%20the%20code%20for%20the%20appropriate%20sheet)%3A%3C%2FP%3E%3CPRE%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%3CBR%20%2F%3EDim%20ChangeCells%20As%20Range%3CBR%20%2F%3ESet%20ChangeCells%20%3D%20Range(%22C2%3AC10%22)%3CBR%20%2F%3EIf%20Not%20Application.Intersect(ChangeCells%2C%20Range(Target.Address))%20Is%20Nothing%20Then%3CBR%20%2F%3EIf%20Target.Value%20%3D%20%22%22%20Then%3CBR%20%2F%3ETarget.Value%20%3D%201%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3EEnd%20Sub%3C%2FPRE%3E%3CP%3EJust%20change%20the%20change%20cells%20to%20the%20appropriate%20range.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20the%20protected%20workbook%20part%20you%20can%20unlock%20via%20the%20vba%20and%20then%20lock%20at%20the%20end%20of%20your%20macro%2C%20For%20workbook%20protection%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EActiveWorkbook.Unprotect%20Password%3A%3D%22Password%22%3CBR%20%2F%3E%3CBR%20%2F%3EActiveWorkbook.Protect%20Password%3A%3D%22Password%22%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFor%20worksheet%20protection%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EActiveSheet.Unprotect%20Password%3A%3D%22Password%22%3CBR%20%2F%3E%3CBR%20%2F%3EActiveSheet.Protect%20Password%3A%3D%22Password%22%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EOr%20better%20yet%2C%20you%20can%20lock%20it%20down%20only%20for%20the%20user%20interface%20giving%20VBA%20full%20Access%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%20ActiveSheet.Protect%20Password%3A%3D%22Password%22%2C%20UserInterFaceOnly%3A%3DTrue%3CBR%20%2F%3EActiveWorkbook.Protect%20Password%3A%3D%22Password%22%2C%20UserInterFaceOnly%3A%3DTrue%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-469524%22%20slang%3D%22en-US%22%3ERe%3A%20Default%20Cell%20value%20%26amp%3B%20Protecting%20a%20worksheet%20that%20uses%20macros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-469524%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F253376%22%20target%3D%22_blank%22%3E%40JWR1138%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20there%2C%20thanks%20for%20your%20reply.%20I%20just%20have%20a%20couple%20of%20questions.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20implemented%20your%20code%20for%20the%20default%20value%20however%20it%20seems%20to%20only%20work%20when%20selecting%20and%20deleting%20one%20cell%20at%20a%20time.%20Is%20there%20any%20way%20to%20have%20this%20work%20when%20selecting%20multiple%20cells%20in%20the%20range%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAlso%20with%20the%20user%20interface%20only%20protection%20I%20keep%20getting%20a%20compile%20error%3A%20invalid%20outside%20procedure%20message%20which%20then%20points%20to%20the%20password%20as%20being%20an%20issue.%20Any%20ideas%20on%20this%20one%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-473170%22%20slang%3D%22en-US%22%3ERe%3A%20Default%20Cell%20value%20%26amp%3B%20Protecting%20a%20worksheet%20that%20uses%20macros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-473170%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%20Sorry%2C%20I%20didn't%20think%20about%20deleting%20multiple%20cells%20at%20once%2C%20try%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EPrivate%20Sub%20Worksheet_Change(ByVal%20Target%20As%20Range)%3CBR%20%2F%3EDim%20ChangeCells%20As%20Range%3CBR%20%2F%3EDim%20cel%20As%20Range%3CBR%20%2F%3ESet%20ChangeCells%20%3D%20Range(%22C2%3AC10%22)%3CBR%20%2F%3EIf%20Not%20Application.Intersect(ChangeCells%2C%20Range(Target.Address))%20Is%20Nothing%20Then%3CBR%20%2F%3EFor%20Each%20cel%20In%20Target%3CBR%20%2F%3EIf%20Not%20Application.Intersect(cel%2C%20ChangeCells)%20Is%20Nothing%20Then%3CBR%20%2F%3EIf%20cel.Value%20%3D%20%22%22%20Then%3CBR%20%2F%3Ecel.Value%20%3D%201%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3ENext%20cel%3CBR%20%2F%3EEnd%20If%3CBR%20%2F%3EEnd%20Sub%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-473210%22%20slang%3D%22en-US%22%3ERe%3A%20Default%20Cell%20value%20%26amp%3B%20Protecting%20a%20worksheet%20that%20uses%20macros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-473210%22%20slang%3D%22en-US%22%3E%3CP%3EI%20may%20have%20you%20given%20some%20bad%20advice%20on%20this%3A%26nbsp%3B%3C%2FP%3E%3CP%3EActiveWorkbook.Protect%20Password%3A%3D%22Password%22%2C%20UserInterFaceOnly%3A%3DTrue%3C%2FP%3E%3CP%3Eas%20that%20doesn't%20seem%20to%20actually%20work%2C%20I%20thought%20it%20did%2C%20sorry.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EProtecting%20the%20sheets%20that%20way%20works%20for%20me%20using%3A%26nbsp%3B%3C%2FP%3E%3CP%3EActiveSheet.Protect%20Password%3A%3D%22Password%22%2C%20UserInterFaceOnly%3A%3DTrue%3C%2FP%3E%3CP%3Ethough%2C%20how%20are%20you%20running%20that%20line%3F%20It%20needs%20to%20be%20stuck%20into%20a%20subroutine%20or%20run%20through%20the%20intermediate%20window%20(I'd%20just%20do%20it%20through%20the%20intermediate%20window%20as%20I%20presume%20you'd%20just%20run%20it%20once%20prior%20to%20distribution).%20Let%20me%20know%20how%20you%20are%20trying%20to%20utilize%20this%20and%20we%20can%20go%20from%20there.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-473673%22%20slang%3D%22en-US%22%3ERe%3A%20Default%20Cell%20value%20%26amp%3B%20Protecting%20a%20worksheet%20that%20uses%20macros%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-473673%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F253376%22%20target%3D%22_blank%22%3E%40JWR1138%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%20again%2C%20First%20off%20the%20default%20cell%20code%20above%20worked%20so%20thanks%20for%20that.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20I'm%20trying%20to%20do%20with%20the%20protection%20is%20making%20it%20so%20that%20only%20certain%20cells%20are%20editable%20(highlighted%20below)%20and%20the%20rest%20of%20the%20sheet%20is%20uneditable%20(to%20protect%20formulae%20and%20the%20functionality%20of%20the%20sheet).%20The%20thing%20is%20I%20have%20a%20clear%20the%20form%20button%20which%20will%20clear%20everything%20in%20columns%20B%20%26amp%3B%20C.%20This%20uses%20a%20Macro%20to%20do%20so%2C%20so%20I%20can't%20protect%20using%20the%20built-in%20functionality.%20It%20sounds%20like%20locking%20down%20the%20UI%20might%20be%20the%20best%20way%20to%20go%20as%20long%20as%20the%20user%20can%20still%20use%20the%20drop%20down%20box%20in%20column%20B%20and%20edit%20the%20data%20in%20column%20C.%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F109554iF6DA88FD08985B64%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22unprotected%20cells.png%22%20title%3D%22unprotected%20cells.png%22%20%2F%3E%3C%2FSPAN%3E%20C%3C%2FP%3E%3C%2FLINGO-BODY%3E
Orii120
Occasional Contributor

Hi All,

 

I'm back again working on this meal planner. Thanks to the assistance of some users here I have been able to get it up and running for the most part. However, I am stuck on two things. 

 

I would like a range of cells to have a default value so that when data is deleted from those cells the sheet will reset the default to 1 instead of becoming blank.

 

default value.png

I would like all of the cells with the value of one in column C to reset to a default value when erased (minus the ones that say grams).

 

I would also like to be able to protect this workbook from editing in all cells on sheet 1 bar columns A, B and K2, L2, N2 and M2 as these cells will be used to input data from the user. The problem is that I have a Macro running to clear columns A and B so using protected mode seems to be out of the question. 

 

Any Ideas?

 

Thanks.

 

5 Replies

Hi, For the default value you could do this (Needs to be on the code for the appropriate sheet):

Private Sub Worksheet_Change(ByVal Target As Range)
Dim ChangeCells As Range
Set ChangeCells = Range("C2:C10")
If Not Application.Intersect(ChangeCells, Range(Target.Address)) Is Nothing Then
If Target.Value = "" Then
Target.Value = 1
End If
End If
End Sub

Just change the change cells to the appropriate range.

 

For the protected workbook part you can unlock via the vba and then lock at the end of your macro, For workbook protection:

 

ActiveWorkbook.Unprotect Password:="Password"

ActiveWorkbook.Protect Password:="Password"

 

For worksheet protection:

 

ActiveSheet.Unprotect Password:="Password"

ActiveSheet.Protect Password:="Password"

 

Or better yet, you can lock it down only for the user interface giving VBA full Access:

 

 ActiveSheet.Protect Password:="Password", UserInterFaceOnly:=True
ActiveWorkbook.Protect Password:="Password", UserInterFaceOnly:=True

 

@JWR1138 

Hi there, thanks for your reply. I just have a couple of questions.

 

I've implemented your code for the default value however it seems to only work when selecting and deleting one cell at a time. Is there any way to have this work when selecting multiple cells in the range?

 

Also with the user interface only protection I keep getting a compile error: invalid outside procedure message which then points to the password as being an issue. Any ideas on this one?

 

Thanks

Hi, Sorry, I didn't think about deleting multiple cells at once, try this:

 

Private Sub Worksheet_Change(ByVal Target As Range)
Dim ChangeCells As Range
Dim cel As Range
Set ChangeCells = Range("C2:C10")
If Not Application.Intersect(ChangeCells, Range(Target.Address)) Is Nothing Then
For Each cel In Target
If Not Application.Intersect(cel, ChangeCells) Is Nothing Then
If cel.Value = "" Then
cel.Value = 1
End If
End If
Next cel
End If
End Sub

 

I may have you given some bad advice on this: 

ActiveWorkbook.Protect Password:="Password", UserInterFaceOnly:=True

as that doesn't seem to actually work, I thought it did, sorry. 

 

Protecting the sheets that way works for me using: 

ActiveSheet.Protect Password:="Password", UserInterFaceOnly:=True

though, how are you running that line? It needs to be stuck into a subroutine or run through the intermediate window (I'd just do it through the intermediate window as I presume you'd just run it once prior to distribution). Let me know how you are trying to utilize this and we can go from there. 

@JWR1138 

 

Hi again, First off the default cell code above worked so thanks for that.

 

What I'm trying to do with the protection is making it so that only certain cells are editable (highlighted below) and the rest of the sheet is uneditable (to protect formulae and the functionality of the sheet). The thing is I have a clear the form button which will clear everything in columns B & C. This uses a Macro to do so, so I can't protect using the built-in functionality. It sounds like locking down the UI might be the best way to go as long as the user can still use the drop down box in column B and edit the data in column C.unprotected cells.png C