在工作簿打开时,从逗号分隔值创建下拉列表

时间:2018-04-12 09:13:14

标签: excel vba excel-vba

我有一列有从DB填充的逗号分隔值。范围可以是任何东西。

enter image description here

当打开工作簿时,逗号分隔列应使用vba下拉值。

我对这个宏很新。我写了下面的代码。它适用于单细胞。但是,我想在某些范围内执行此操作,例如从H1到最后的示例“H”列。怎么做到这一点?可以帮帮我吗?

Private Sub Workbook_Open()
    AddListValidation "Task", "A1", "A2"
End Sub


Sub AddListValidation(sheet, cellSource As Range, cellTarget As Range)
    Dim Lastrow As Integer
    Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row


    txt = ActiveWorkbook.Worksheets(sheet).Range(cellSource).Value
    ActiveWorkbook.Worksheets(sheet).Range(cellTarget) = "Select your values here"
    With ActiveWorkbook.Worksheets(sheet).Range(cellTarget).Validation
        .Delete
        .Add Type:=xlValidateList, Formula1:="xxx,yyy,zzz"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

您需要一个循环来为每一行调用test string="", sha3.keccak_256()="c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" test string="", hashlib.keccak_256()="a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a" test string="eth", sha3.keccak_256()="4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f0" test string="eth", hashlib.keccak_256()="4b3cdfda85c576e43c848d43fdf8e901d8d02553fec8ee56289d10b8dc47d997" test string="foo.bar", sha3.keccak_256()="461e2b648c9a6c0c3e2cab45884ae0fcab21c655fcf588f2a45e6596e3e0e9a7" test string="foo.bar", hashlib.keccak_256()="e55dea66e750540f599874a18596745b0c5705bc6873ca3ef1ccd2acbba88670" 。当您将完全限定范围作为参数而不是地址时,也不需要工作表的参数。

在VBA中建议always to use Long instead of Integer使用AddListValidation没有任何好处。特别是对于行计数,因为Excel的行数多于Integer可以处理的行数。

Integer