以下内容只会在当前工作表中查看,但可以扩展到循环遍历所有工作表。它也只检查列表类型的验证。
Sub ClearListValidations()
Dim ws As Worksheet 'not used currently
Dim rngCurrent As Range
Dim rngValids As Range
Dim strLookup As String
Dim rng As Range
Dim varIntersect As Variant
Application.ScreenUpdating = False
Set rngCurrent = ActiveCell
On Error Resume Next
Set rngValids = rngCurrent.SpecialCells(xlCellTypeAllValidation)
On Error GoTo 0 'reinstate error handling
If rngValids Is Nothing Then Exit Sub 'no list validations
For Each rng In rngValids
If rng.Validation.Type = 3 Then 'list validation
Set varIntersect = Application.Intersect(rngCurrent, _
Range(Mid(rng.Validation.Formula1, 2)))
If Not varIntersect Is Nothing Then
rng.ClearContents
rng.Validation.Delete
End If
End If
Next rng
Application.ScreenUpdating = True
End Sub
本质上,如果 List-Validation 的源与当前单元格相交,它会从单元格中删除内容和验证设置。
使用该MID()
函数是因为Formula1
包含一个等号,我们需要将其丢弃以形成一个 Range。