Search code examples
vbaexcelexcel-2013

VBA Run-time error '1004' in a routine that has worked flawlessly


I have a protected form that organizes user input from open cells onto another excel sheet and emails it to specific people. This form has worked flawlessly for years, but suddenly yesterday it's throwing a Run-time error '1004': Application-defined or object-defined error. When I enter anything into the form, the error appears immediately, and the debugger highlights the following line of code.

Range("AL6").Font.Color = vbWhite

On the form where a user adds their information, the cells that are open to users range from AF6-AK6 and auto-populate tomorrows date. AL6 is outside this and protected. There is nothing in cell 'AL6' and nor should there be. This is the code block in which the error dwells.

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
       If Time > 15 / 24 And Range("AF6") = Application.WorksheetFunction.WorkDay(Date, 0) Then
           Range("AL6").Font.Color = vbRed
       Else
           Range("AL6").Font.Color = vbWhite
       End If

 End Sub

The form is also saved regularly, so I don't think it's a matter of duplicating itself and becoming corrupted. I also cannot rule that possibility out. Any thoughts?


Solution

  • The issue appears to be related to sheet protection.

    You could either set the cell "AL6" to locked = false, or you can use the Workbook_Open event to apply user interface only locking, which will allow VBA to run as if it is unlocked:

    For example:

    Private Sub Workbook_Open()
    
        Thisworkbook.sheets("<put your sheet name here>").Protect "Password", UserInterfaceOnly := True
    
    End Sub
    

    More info on UserInterfaceOnly: Protecting cells in Excel but allow these to be modified by VBA script