Macro error with protected sheet, debug flagging color

phownz

New Member
Joined
Jul 28, 2021
Messages
21
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I'm having an issue whereby when protecting a sheet my macro (responsible for highlighting a cell with a colour when a word limit is exceeded at 200, 350 and 500, each within a separate column) results in a "Run-time-error 1004"

1634560478285.png


The debug highlights this section of the code:

1634560612999.png


The full code is here:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Column >= 24 And Target.Column <= 26 Then
        Dim txt As String
        Dim lim As Long
        
        txt = ""
        
        Select Case Target.Column
        Case 24
            lim = 200
            If Len(Target.Value) > lim Then
                Target.Interior.Color = 65535
                txt = txt & vbCrLf & Target.Address(0, 0)
                                
            End If
        
        Case 25
            lim = 350
            If Len(Target.Value) > lim Then
                Target.Interior.Color = 65535
                txt = txt & vbCrLf & Target.Address(0, 0)
            End If
        
        Case 26
            lim = 500
             If Len(Target.Value) > lim Then
                Target.Interior.Color = 65535
                txt = txt & vbCrLf & Target.Address(0, 0)
            End If
        End Select
        
        If txt <> "" Then MsgBox "Cell exceeds character limit of " & CStr(lim) & vbCrLf & txt
    End If
End Sub

The macro works without any issues when the sheet is unprotected, the cells the macro is responsible for are UNPROTECTED as data will be copy and pasted in.

I've read some posts re: allowing the VBA code to unprotect the sheet before running the macro but this seemed to be related to locked cells.

Do I have to apply the same sort of logic to my code? And if so what would I need to add?

Thanks in advance.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You could either start and end your procedure with
VBA Code:
    ' start
    Target.Parent.Unprotect Password:="YoUrPaSsWoRd"
    '
    ' your code to do some stuff
    '
    ' end
    Target.Parent.Protect Password:="YoUrPaSsWoRd"

or you could protect your worksheet only for change attempts through the user interface, which you only have to do once.:
VBA Code:
    ThisWorkbook.Sheet("Sheet1").Protect Password:="YoUrPaSsWoRd", UserInterfaceOnly:=True
 
Upvote 0
Hi GWteb,

Thanks for the quick reply. I've tried the first option and that works well.

I'm struggling a bit with the second, probably where I'm placing it as I get a compile error. Where would that need to be called in the macro?

Thanks
 
Upvote 0
The second one needs just to be invoked once. It errors on already protected sheets. So unprotect the worksheet involved, invoke this code once and save your workbook. Done!
From now on users can't change anything while macros can.
 
Upvote 0
Perfect! My worksheet was protected beforehand... Both work, and many thanks!
 
Upvote 0
You're welcome and thanks for the follow-up (y)
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,560
Members
449,089
Latest member
Motoracer88

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