[英]'1004' error on While iteration with Interior.Color Excel 2010 VBA ActiveX Controls
[英]Run time error 1004 “Interior.Color”
我正在以编程方式创建Excel工作簿,并将其与按钮关联了一个宏,该宏应该检查用户在工作表中输入的值是否正确,并根据情况将单元格涂成绿色或红色。
宏的代码在另一个Excel工作簿中,并使用以下代码添加到创建的工作簿中:
With newWorkBook.Worksheets(1).Buttons.Add(350, 115, 50, 41.25)
.Caption = "Vérifier la conformité"
.OnAction = "'" & ThisWorkbook.FullName & "'!check_FCM"
End With
这是宏的无效代码部分:
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对象更改颜色。 核实。 我对您的代码做了一些小的修改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.