我在Excel表单上有一个下拉列表,我希望用“OR”表中列出的项目填充,但是我只需要此动态列表中的唯一条目&然后填充下拉列表。
搜索了很多博客,建议将命名范围作为Rowsource属性,但我的列表中有重复的项目&是动态的。
答案 0 :(得分:1)
您可以编写此代码:
Dim s As String, r As Integer, nr As Integer, wr, v
Set wr = Range("A1:A10")
nr = wr.Rows.Count
For r = 1 To nr
v = wr(r, 1)
If InStr(s, v & ",") = 0 Then
s = s & v & ","
End If
Next
s = Left(s, Len(s) - 1)
With Range("D1").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:=s
End With
这使用数据验证,我假设数据在A1:A10和D1中的ComboBox。 ComboBox将仅包含A1:A10不同的值
如果您更喜欢ComboBox ActiveX对象,请尝试:
Dim s As String, r As Integer, nr As Integer, wr, v
Set wr = Range("A1:A10")
nr = wr.Rows.Count
With ComboBox1
.Clear
For r = 1 To nr
v = wr(r, 1)
If InStr(s, v & ",") = 0 Then
s = s & v & ","
.AddItem (v)
End If
Next
End With