How to pass values from an array to a drop down li

2020-04-15 11:52发布

I'm trying to pass values from an array to a drop down list in a specifc cell. Say I have an array which contains the values 1,2,3 and I want cell A1 to contain a drop down list with these value, Is there any way I can do this? I'm trying to achive this without having to first assign these value to different cells and use data validation. (And as stated in the title, I don't want to use combo boxes or user forms)

2条回答
甜甜的少女心
2楼-- · 2020-04-15 12:34

This should give you a way of doing it:

Dim myArray
myArray = Array("1", "2", "3")

Range("A" & 1).Select

With Selection.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:=myArray(0) & "," & myArray(1) & "," & myArray(2)
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = True
End With
查看更多
再贱就再见
3楼-- · 2020-04-15 12:49

Here's a couple of ways, same result,

Sub DataVal1()
    With Range("A1").Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
             xlBetween, Formula1:="1,2,3"
    End With
End Sub
Sub DataVal2()
    Dim x As String
    x = "1,2,3"
    With Range("A1").Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
             xlBetween, Formula1:=x
    End With
End Sub
查看更多
登录 后发表回答