简体   繁体   English

运行时错误1004“ Interior.Color”

[英]Run time error 1004 “Interior.Color”

I'm programmatically creating Excel workbooks, with a macro associated to a button, this macro is supposed to check if the values entered by the user in the worksheet are correct and to color the cells in green or red depending on the case. 我正在以编程方式创建Excel工作簿,并将其与按钮关联了一个宏,该宏应该检查用户在工作表中输入的值是否正确,并根据情况将单元格涂成绿色或红色。

The macro's code is in another Excel workbook and is added to the created workbooks with this code : 宏的代码在另一个Excel工作簿中,并使用以下代码添加到创建的工作簿中:

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

Here is the macro's code part that doesn't work : 这是宏的无效代码部分:

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 '

The problem seems to be coming from the use of " Interior.Color=RGB(x,y,z) " because when I remove it I don't get a bug. 该问题似乎是由于使用了“ Interior.Color=RGB(x,y,z) ”引起的,因为当我删除它时,我没有发现错误。

You can Unprotect and Protect sheet as follow: 您可以按以下步骤取消保护保护工作表:

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

And also can use Cells object to change color. 并且还可以使用Cells对象更改颜色。 Check it. 核实。 I made small modification to your code. 我对您的代码做了一些小的修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM