VBA以编程方式在添加数据验证列表时将值放入单元格中

时间:2016-05-19 23:44:29

标签: vba excel-vba validation excel

我非常精通VBA,但不确定这是否可行,因为在搜索过去的15分钟时我找不到任何东西。

我以通常的方式添加数据验证列表

startCell.Offset(1, 2).Select
With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:="=Categories"
    .IgnoreBlank = True
    .InCellDropdown = True        
End With

但是当例程结束时,没有选择任何值,因此单元格是空白的。

有没有办法在添加数据验证时设置单元格值,即默认值可能是数据验证列表中的第一项?

1 个答案:

答案 0 :(得分:1)

试试这个:

With startCell.Offset(1, 2)
    With .Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
             Operator:= xlBetween, Formula1:="=Categories"
        .IgnoreBlank = True
        .InCellDropdown = True
    End With
    .Value = Range("Categories").Cells(1).Value
End With