简体   繁体   English

使用Excel VBA创建下拉列表

[英]Create Drop-down List with Excel VBA

I want to create a drop-down list in Cell E5 . 我想在Cell E5创建一个下拉列表。

My range of values for the drop-down should come from Range(A1:A5) in Sheet2 and Range(C1:C7) in Sheet2 . 我给下拉值的范围应来自Range(A1:A5)Sheet2Range(C1:C7)Sheet2 However, my code below is not working, it runs an error 1004 at Add xlValidateList, xlValidAlertStop, Operator:=xlBetween, Formula1:="=Sheet2!$A$1:$A$5 & Sheet2!$C$1:$C$7" I suspect it has something to do with this line... Formula1:="=Sheet2!$A$1:$A$5 & Sheet2!$C$1:$C$7" . 但是,下面的代码无法正常工作,它在Add xlValidateList, xlValidAlertStop, Operator:=xlBetween, Formula1:="=Sheet2!$A$1:$A$5 & Sheet2!$C$1:$C$7"时运行错误1004我怀疑这与这条线有关... Formula1:="=Sheet2!$A$1:$A$5 & Sheet2!$C$1:$C$7" Does anyone know how I can add two ranges into my Formula1 ? 有人知道我如何在Formula1添加两个范围吗?

Also, I dont want the blanks in the ranges to appear in the Drop-down list but .IgnoreBlank = True still shows the blanks in the drop-down list. 另外,我不希望范围内的空白出现在下拉列表中,但是.IgnoreBlank = True仍会在下拉列表中显示空白。 This is my code thus far, any help is genuinely appreciated: 到目前为止,这是我的代码,真诚地感谢您的帮助:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Target, Range("E5")) Is Nothing Then

With Range("e5").Validation
.Add xlValidateList, xlValidAlertStop, Operator:=xlBetween, Formula1:="=Sheet2!$A$1:$A$5 & Sheet2!$C$1:$C$7"
.IgnoreBlank = True
.InCellDropdown = True
.ErrorTitle = "Warning"
.ErrorMessage = "Please select a value from the drop-down list available."
.ShowError = True

End With

End If

End Sub

To add multiple columns data, use a unique collection and then feed that to the DV in a comma delimited string. 要添加多列数据,请使用唯一的集合,然后以逗号分隔的字符串将其提供给DV。 See this example. 请参阅此示例。 Change as applicable. 进行相应的更改。

Sub Sample()
    Dim col As New Collection
    Dim rng As Range
    Dim i As Long
    Dim DVList As String

    '~~> Loop through the data range
    For Each rng In Sheet2.Range("A1:A5,C1:C5")
        '~~> Ignore blanks
        If Len(Trim(rng.Value)) <> 0 Then
            '~~> Create a unique list
            On Error Resume Next
            col.Add rng.Value, CStr(rng.Value)
            On Error GoTo 0
        End If
    Next rng

    '~~> Concatenate with "," as delimiter
    For i = 1 To col.Count
        DVList = DVList & col.Item(i) & ","
    Next i

    '~~> Feed it to the DV
    With Sheet1.Range("E5").Validation
        .Delete
        .Add Type:=xlValidateList, _
        AlertStyle:=xlValidAlertStop, _
        Formula1:=DVList
    End With
End Sub

the property Formula1:=" Expression " does not allow splitted tables. 属性Formula1:=“ Expression ”不允许拆分表。 As Siddharth Rout wrote - as a work aorund - first you have to collect all values in a single table or a string. 正如Siddharth Rout所写的那样-作为一项工作重点-首先,您必须将所有值收集在一个表或字符串中。

Also the IgnoreBlank property does not ignore blanks, but is considering the cell value as valid even the cell content is blank. 同样,IgnoreBlank属性不会忽略空格,而是将单元格值视为有效,即使单元格内容为空白。 So, while collecting table content skip blank cells. 因此,在收集表内容时,跳过空白单元格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM