Protected Worksheet Not Working with VB Code

KMS126

New Member
Joined
Nov 28, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello!

I am trying to update a worksheet that is supposed to be protected for others to use. I unprotected the worksheet, entered my code, protected the worksheet, then saved it to be Macro compatible and now the code doesn't work.

I did not use a password to protect it. Do I need one?

Current code was derived from a post I found on here from a few years ago. I have not worked with VB code before so this is all new to me!:

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Count = 1 Then
Dim Colr As Long, Txt As String
If Target.Address(0, 0) = "A5" Then
Txt = "Parent -- Employer"
ElseIf Target.Address(0, 0) = "A14" Then
Txt = "Parent -- Employer"
ElseIf Target.Address(0, 0) = "A23" Then
Txt = "Parent -- Employer"
ElseIf Target.Address(0, 0) = "A30" Then
Txt = "Parent -- Employer"
ElseIf Target.Address(0, 0) = "A37" Then
Txt = "Parent -- Employer"
ElseIf Target.Address(0, 0) = "A44" Then
Txt = "Parent -- Employer"
ElseIf Target.Address(0, 0) = "A51" Then
Txt = "Income Type"
End If

Application.EnableEvents = False
If Len(Target.Value) = 0 Or Target.Value = Txt Then
Target.Font.ColorIndex = 15
Target.Value = Txt
Target.Font.Bold = False
Else
Target.Font.ColorIndex = 13
Target.Font.Bold = True

End If
Application.EnableEvents = True
End If

End Sub



(Summary: I am trying to have specific cells be light in color until someone types in that cell to add information and have it change colors and be bold).
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Welcome to the Board!

If you have protected your worsheet, then you will need to unprotect it at the beginning of you VBA code like this:
VBA Code:
ActiveSheet.Unprotect

Then you can re-protect it again at the end of your code like this:
VBA Code:
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

Note that you can get these lines of code simply by using the Macro Recorder and recording yourself protecting/unprotecting the sheet.
 
Upvote 0
Welcome to the Board!

If you have protected your worsheet, then you will need to unprotect it at the beginning of you VBA code like this:
VBA Code:
ActiveSheet.Unprotect

Then you can re-protect it again at the end of your code like this:
VBA Code:
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

Note that you can get these lines of code simply by using the Macro Recorder and recording yourself protecting/unprotecting the sheet.
Thank you! This worked perfectly!

Now I need to figure out how to fix it so only certain cells change text color instead of everything turning purple!
 
Upvote 0
Thank you! This worked perfectly!

Now I need to figure out how to fix it so only certain cells change text color instead of everything turning purple!
That is because your second block of code here (unlike your first block) is applied to ALL cells instead of just certain ones:
VBA Code:
If Len(Target.Value) = 0 Or Target.Value = Txt Then
Target.Font.ColorIndex = 15
Target.Value = Txt
Target.Font.Bold = False
Else
Target.Font.ColorIndex = 13
Target.Font.Bold = True

End If
Notice how in the first block your are checking the cell addresses so it is only applying it to certain cells.
Your second block of code has no such conditions.
 
Upvote 0
That is because your second block of code here (unlike your first block) is applied to ALL cells instead of just certain ones:
VBA Code:
If Len(Target.Value) = 0 Or Target.Value = Txt Then
Target.Font.ColorIndex = 15
Target.Value = Txt
Target.Font.Bold = False
Else
Target.Font.ColorIndex = 13
Target.Font.Bold = True

End If
Notice how in the first block your are checking the cell addresses so it is only applying it to certain cells.
Your second block of code has no such conditions.

Ok that makes sense. I figured it had to do with the second part. Any recommendations on how I can fix it? I only need the specific A cells (from the first part) to change to the bold and ColorIndex 13.
 
Upvote 0
OK, I took some liberties of re-writing/simplifying your code.

See if this does all that you want:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim txt As String
    Dim chk As Boolean

    If Target.CountLarge > 1 Then Exit Sub

    ActiveSheet.Unprotect
    Application.EnableEvents = False
    
    chk = False
    
    Select Case Target.Address(0, 0)
        Case "A5", "A14", "A23", "A30", "A37", "A44"
            If (Target.Value = "Parent -- Employer") Or (Len(Target.Value) = 0) Then
                txt = "Parent -- Employer"
                chk = True
            End If
        Case "A51"
            If (Target.Value = "Income Type") Or (Len(Target.Value) = 0) Then
                txt = "Income Type"
                chk = True
            End If
    End Select
    
    If chk = True Then
        Target.Font.ColorIndex = 15
        Target.Value = txt
        Target.Font.Bold = False
    Else
        Target.Font.ColorIndex = 13
        Target.Font.Bold = True
    End If

    Application.EnableEvents = True
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    
End Sub
 
Upvote 0
OK, I took some liberties of re-writing/simplifying your code.

See if this does all that you want:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim txt As String
    Dim chk As Boolean

    If Target.CountLarge > 1 Then Exit Sub

    ActiveSheet.Unprotect
    Application.EnableEvents = False
   
    chk = False
   
    Select Case Target.Address(0, 0)
        Case "A5", "A14", "A23", "A30", "A37", "A44"
            If (Target.Value = "Parent -- Employer") Or (Len(Target.Value) = 0) Then
                txt = "Parent -- Employer"
                chk = True
            End If
        Case "A51"
            If (Target.Value = "Income Type") Or (Len(Target.Value) = 0) Then
                txt = "Income Type"
                chk = True
            End If
    End Select
   
    If chk = True Then
        Target.Font.ColorIndex = 15
        Target.Value = txt
        Target.Font.Bold = False
    Else
        Target.Font.ColorIndex = 13
        Target.Font.Bold = True
    End If

    Application.EnableEvents = True
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
   
End Sub
Unfortunately it's still changing the font on all the other cells.

(But this code is a lot cleaner and easier to understand, thank you!)
 
Upvote 0
Please show me an example of your sheet/data, and your expected behavior.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Calculation Worksheet (Dec 2022).xlsm
ABCDEFGHI
1UPDATED INCOME CALCULATION SHEET 4/2021
2Six Weeks Back Date:____________________________FID:____________________________
3
4WEEK INCOME JOB #1:
5Parent -- EmployerHOURSDATESelf Employed Quarterly Earnings
6Month/YearNet IncomeHour/$14.25
70.00
80.00
90.00
10$0.00$0.000.00$0.000.00
11TotalMonthy AvgWeek AvgMonthy AvgAvg Weekly
12
13WEEK INCOME JOB #2:
14Parent -- EmployerHOURSDATEOther Income 1:___________________________________DATE
15MonthlyWeekBiweek
16
17
18
19$0.00$0.000.00
20TotalMonthy AvgWeek Avg$0.00$0.00
21$0.00$0.00
22BI-WEEK INCOME JOB #1:$0.00$0.00
23Parent -- EmployerHOURSDATE
24
25Other Income 2:___________________________________DATE
26$0.00$0.000.00MonthlyWeekBiweek
27TotalMonthy AvgWeek Avg
28
29BI-WEEK INCOME JOB #2:
30Parent -- EmployerHOURSDATE
31$0.00$0.00
32$0.00$0.00
33$0.00$0.000.00$0.00$0.00
34TotalMonthy AvgWeek Avg
35
36TWICE MONTH INCOME JOB #1:
37Parent -- EmployerHOURSDATEChild Support Paid Out (enter as negative)DATE
38MonthlyWeekBiweek
39
40$0.00$0.000.00
41TotalMonthy AvgWeek Avg
42
43TWICE MONTH INCOME JOB #2:$0.00$0.00
44Parent -- EmployerHOURSDATE$0.00$0.00
45$0.00$0.00
46
47$0.00$0.000.00
48TotalMonthy AvgWeek Avg
49
50MONTHLY Income: Net Rental IncomeMonth
51Income TypeHOURSDATETotal Rents
52Utilities
53Mortgage
54Taxes
55$0.00
56MONTHLY INCOME CALCULATION negative income enter as 0
57TOTAL GMIADJUSTED GMI
58TAFDCExplain Other
59$0.00Other Income 1Explain Other
60$0.00Other Income 2Gross Monthly Income
61Net Rental Income Received$0.00Child Support/Alimony Paid
62Any other IncomeTAFDC Rental Allowance
63$0.00Monthly IncomeEmployee Benefits
64$0.00Total Gross Monthly Income$0.00Total GMI To Deduct
Sheet1
Cell Formulas
RangeFormula
H7:H9H7=SUM(G7/4.33)/(14.25)
A10,G43:H43,G31:H31,G20:H20,A19A10=SUM(A6:A9)
B10,B19B10=SUM(A10/4*4.33)
C10,C19C10=SUM(C6:C9)/(4)
G10:H10G10=SUM(G7:G9)/(3)
G21,G44,G32G21=G20/4
H21,H44,H32H21=H20/2
G22,G45,G33G22=G21*4.33
H22,H45,H33H22=H21*2.17
A26,A47,A40,A33A26=SUM(A24:A25)
B26,B33B26=SUM(A26/2*2.17)
C26,C47,C40,C33C26=SUM(C24:C25)/(4)
B40,B47B40=SUM(A40)
H55H55=H51-H52-H53-H54
A59A59=SUM(F16+G22+H22)
A60A60=SUM(F27+G33+H33)
D61D61=SUM(F39+G45+H45)
A63A63=SUM(B10+B19+B26+B33+B40+B47+A52+G10)
A64A64=SUM(A58:A63)+(D64)
D64D64=SUM(D58:D63)
Cells with Data Validation
CellAllowCriteria
A5Any value



Please show me an example of your sheet/data, and your expected behavior.

MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top