[英]'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.