将值粘贴到工作表中后创建下拉列表槽列

时间:2020-03-12 11:00:13

标签: excel vba

因此,在将新数据粘贴到工作表中之后,我试图通过整个列创建一个下拉列表。因此,粘贴操作将触发在最后一列中创建下拉列表。 现在,我已经编写了代码,但是它不起作用。有帮助吗?

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rng As Range, r1 As Range
Set Rng = Range("AQ7:AQ10000")
If Intersect(Target, Rng) Is Nothing Then Exit Sub
For Each r1 In Rng
r1.Select
 With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="=YES"
    .IgnoreBlank = True
    .InCellDropdown = True
    .ShowInput = True
    .ShowError = True
 End With
Next

1 个答案:

答案 0 :(得分:0)

对于Static List Values:您应该使用Formula1:="Yes,No"

对于Dynamic List Values:首先将下拉列表存储在cells中,然后使用该列表。或者,您也可以定义named range,然后在Formula中使用它。

.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=Sheet1!B1:B6"
相关问题