正常运行的例程中的VBA运行时错误'1004'

时间:2018-09-18 18:10:43

标签: excel vba excel-vba excel-2013

我有一个受保护的表单,可以将用户输入的内容从打开的单元格组织到另一个Excel工作表中,并通过电子邮件发送给特定的人。这种形式已经完美地运行了很多年,但是昨天突然出现了一个运行时错误“ 1004”:应用程序定义的错误或对象定义的错误。当我在表单中输入任何内容时,错误立即出现,并且调试器突出显示以下代码行。

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

在用户添加信息的表单上,对用户开放的单元格范围为AF6-AK6,并自动填充明天的日期。 AL6不在此范围内并受到保护。单元格“ AL6”中没有任何内容,也不应该有。这是错误所在的代码块。

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

该表格也会定期保存,因此我认为这不是重复和损坏的问题。我也不能排除这种可能性。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

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

相关问题