Share via

Error when using a protected sheet

Fantana, Sean M 0 Reputation points
2025-10-27T20:21:10.7033333+00:00

I am trying to protect a work sheet that has an automatic row highlighter in visual basic already. after protecting the sheet when I click on a cell, I get a run-time error '1004':. How do I protect the sheet (due to formulas I have in cells dealing with the date) and still highlight the rows I am working on?

Microsoft 365 and Office | Excel | For business | Windows
0 comments No comments

2 answers

Sort by: Most helpful
  1. Hendrix-C 15,095 Reputation points Microsoft External Staff Moderator
    2025-10-28T21:12:26.28+00:00

    Hi @Fantana, Sean M,

    Thank you for your prompt update. I have tested and reproduce the issue you're experiencing on my worksheet.

    User's image

    Before protecting the sheet, make sure you double-check these things:

    • Formula cells in column F and H are locked in Format cells > Protection > Locked
    • Other cells where users can click are unlocked.

    Now you can open VBA and use this script:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    'Update by Extendoffice
        Dim rowRange As Range
        Dim colRange As Range
        Dim activeCell As Range
        ActiveSheet.Unprotect "123" 'replace 123 with your password or use "" if no password
        Set activeCell = Target.Cells(1, 1)
        Set rowRange = Rows(activeCell.Row)
        Set colRange = Columns(activeCell.Column)
        Cells.Interior.ColorIndex = xlNone
        rowRange.Interior.Color = RGB(248, 150, 171)
        ActiveSheet.Protect "123", Contents:=True, Scenarios:=True, UserInterfaceOnly:=False
    End Sub
    

    The difference is that using the parameter UserInterfaceOnly. This will allow VBA to modify the sheet when still preventing users from making changes.
    User's image

    User's image

    Please feel free to reach out again if you have any trouble or need further assistance. I'm happy to help.

    Hope to hear from you soon.

    1 person found this answer helpful.
    0 comments No comments

  2. Hendrix-C 15,095 Reputation points Microsoft External Staff Moderator
    2025-10-27T20:56:33.2733333+00:00

    Hi @Fantana, Sean M,

    Thank you for posting your question in the Microsoft Q&A forum.

    According to your concern, the "Run time error '1004'" occurs because macro is trying to modify something that's restricted by Excel sheet protection. You'll need to modify your VBA code to unprotect the sheet temporarily before the highlighting action and then re-protect it afterward.

    I assume your existing row highlighting code is in the Worksheet_SelectionChange event, it will be modified like this:

    Private Sub Worksheet_SelectionChange(ByVal Target as Range)
    ActiveSheet.Unprotect "yourpassword" 'replace with your password
    'your highlight code here
    'example: 
    Target.EntireRow.Interior.Color = RGB(255,255,0)
    ActiveSheet.Protect "yourpassword", _
    DrawingObjects := True, Contents := True, Scenarios:= True, AllowFormattingRows:=True, _
    AllowFormattingColumns:=True
    End Sub
    

    Hope this will help. Please feel free to reach out again if you need help with anything. I'm happy to assist.

    Thank you for your understanding and cooperation. I'm looking forward to your reply.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".   

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.