从工作表中删除唯一验证列表

时间:2016-09-26 13:36:30

标签: excel excel-vba vba

我有以下代码来生成验证列表。作为测试,我将“myList”放在单元格A5中。它并不总是在固定的位置。我想生成一个宏,将删除所有“mylist”验证列表。我无法删除所有验证列表(cells.validation.delete),因为工作表中存在其他列表。任何支持都会非常感激。

谢谢,

夏兰。

    Sub List_Test()

Dim myList$, i%

myList = ""

For i = 1 To 7

    myList = myList & "ListItem" & i & ","

Next i

myList = Mid(myList, 1, Len(myList) - 1)

With Range("A5").Validation
    .Delete
    .Add _
    Type:=xlValidateList, _
    AlertStyle:=xlValidAlertStop, _
    Formula1:=myList
End With

End Sub

1 个答案:

答案 0 :(得分:1)

出于某种原因,你的(可能全部?)VBA不喜欢多行,只需将.Add放在一行:

Sub List_Test()

Dim myList$, i%

myList = ""

For i = 1 To 7

    myList = myList & "ListItem" & i & ","

Next i

myList = Mid(myList, 1, Len(myList) - 1)

With Range("A5").Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=myList
End With

End Sub