运行时错误1004“Interior.Color”

我以编程方式创buildExcel工作簿,与一个button关联的macros,这个macros应该检查用户在工作表中input的值是否正确,并根据情况为单元格以绿色或红色着色。

该macros的代码位于另一个Excel工作簿中,并使用以下代码添加到创build的工作簿中:

With newWorkBook.Worksheets(1).Buttons.Add(350, 115, 50, 41.25) .Caption = "Vérifier la conformité" .OnAction = "'" & ThisWorkbook.FullName & "'!check_FCM" End With 

这是macros的代码部分,不工作:

 For i = 0 To col - 1 If (IsNumeric(Cells(29, i + 2).Value)) Then If (Cells(29, i + 2).Value >= Cells(31, i + 2).Value And Cells(29, i + 2).Value <= Cells(32, i + 2)) Then Range(Cells(29, i + 2).Address()).Interior.Color = RGB(0, 255, 0) Else Range(Cells(29, i + 2).Address()).Interior.Color = RGB(255, 0, 0) isCorrect = False ' End If ' End If ' Next i ' 

这个问题似乎来自使用“ Interior.Color=RGB(x,y,z) ”,因为当我删除它,我没有得到一个错误。

您可以按照以下步骤取消保护保护工作表:

 Sheets("sheetname").Unprotect For i = 0 To col - 1 If (IsNumeric(Cells(29, i + 2).Value)) Then If (Cells(29, i + 2).Value >= Cells(31, i + 2).Value And Cells(29, i + 2).Value <= Cells(32, i + 2)) Then Cells(29, i + 2).Interior.Color = RGB(0, 255, 0) Else Cells(29, i + 2).Interior.Color = RGB(255, 0, 0) isCorrect = False End If End If Next i Sheets("sheetname").Protect 

也可以使用Cells对象来改变颜色。 核实。 我对你的代码做了小小的修改。