Excel VBA – 如果未填充颜色,则将数据validation放置在单元格中

我正在尝试将数据validation添加到没有填充颜​​色的单元格。 用我试过的代码(见下面),由于某种原因,它仍然将数据有效性添加到范围中的每个单元格,即使它们被填充颜色。

任何帮助表示赞赏!

Sub Data_Validation() Dim WS As Worksheet Dim WS2 As Worksheet Dim Range1 As Range, Range2 As Range Dim c As Range Set WS = ThisWorkbook.Worksheets("Report") Set WS2 = ThisWorkbook.Worksheets("List") 'these are two cells in column A, but they may change position if rows are added, so I named them. Set Range1 = WS.Range("DV_Start:DV_End") 'This is the named range for cells on the List worksheet: Set Range2 = WS2.Range("ListCells") For Each c In Range1 If c.Interior.ColorIndex <> xlNone Then Else With Range1.Validation .Delete .Add Type:=xlValidateList, _ Formula1:="='" & WS2.Name & "'!" & Range2.Address .IgnoreBlank = True .InCellDropdown = True .InputTitle = "Name" .ErrorTitle = "ERROR: Invalid" .InputMessage = "Please enter or select something..." .ErrorMessage = "What you have entered is invalid. Please try again." .ShowInput = True .ShowError = True End With End If Next End Sub 

按照Jeeped的评论:

c.interior.colorindex更改为c.interior.colorindex c.interior.pattern更改为c.validation

得到它的工作。 再次感谢Jeeped!