我在一系列单元格中进行动态数据验证,每次选择一个单元格时都会更新目标单元格。例如,我有一个标题为“University”的列,它从另一个页面上的大学列表中提取数据。在单元格中键入或从下拉列表中选择时,验证效果很好。
但是,当我从另一个单元格(例如55555)复制无效值并将其粘贴到University列时,它允许我按Enter键并移动到下一个单元格而不给我提醒。
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strList As String
Dim ws As Worksheet
Dim RefRng As Range, RngFind As Range, NewRng As Range, hdr
Dim RefList As Range, c As Range, rngHeaders As Range, msg
On Error GoTo ErrHandling
Set ws = ThisWorkbook.Worksheets("Candidate Picklist")
'only deal with the selected cell(s)
Set NewRng = Application.Intersect(Me.Range("A12:T101"), Target)
If Not NewRng Is Nothing Then
Set rngHeaders = ws.Range("A11:ZZ11")
For Each c In NewRng
c.Validation.Delete 'delete previous validation
hdr = Me.Cells(11, c.Column).Value
If Len(hdr) > 0 Then
Set RngFind = rngHeaders.Find(hdr, , xlValues, xlWhole)
'matched header?
If Not RngFind Is Nothing Then
Set RefList = ws.Range(RngFind.Offset(1, 0), _
RngFind.Offset(1, 0).End(xlDown))
With c.Validation
.Add Type:=xlValidateList, _
AlertStyle:=xlValidAlertStop, _
Formula1:="='" & ws.Name & "'!" & RefList.Address
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End If 'matched header
End If 'has header
Next c
End If 'in required range