我试图将值从数组传递到特定单元格中的下拉列表。 假设我有一个包含值1,2,3的数组,我希望单元格A1包含一个带有这些值的下拉列表,有什么方法可以做到这一点吗? 我试图实现这一点,而不必先将这些值分配给不同的单元格并使用数据验证。 (正如标题中所述,我不想使用组合框或用户表单)
答案 0 :(得分:2)
这里有两种方法,相同的结果,
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
答案 1 :(得分:2)
这应该会给你一种方法:
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