我正在尝试向不填充颜色的单元格添加数据验证。使用我尝试的代码(见下文),出于某种原因,它仍然会将数据验证添加到范围中的每个单元格,即使它们填充了颜色。
感谢任何帮助!
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
答案 0 :(得分:1)
根据Jeeped的评论:
将c.interior.colorindex
更改为c.interior.pattern
并将Range.Validation
更改为c.validation
让它运转起来。再次感谢Jeeped!