[英]Excel or VBA - populate a drop down list from a dynamic column in another sheet with no duplicates
I don't know if this is even possible, but I am trying to do a couple of things that require me to populate drop down lists, and a normal cell by cell list, based on the items in a column on another sheet.我不知道这是否可能,但我正在尝试做一些事情,这些事情需要我根据另一张工作表上一列中的项目填充下拉列表和一个普通的单元格列表。 I know how to do this with a regular column, but the column I want to use changes length and has many duplicate values in it, as it is an inventory of sorts.我知道如何使用常规列执行此操作,但是我要使用的列更改了长度并且其中包含许多重复值,因为它是各种清单。
So on this sheet, in C5,所以在这张纸上,在 C5 中,
that value should be able to be selected from a drop down list based on the contents of column B in the second sheet, but also without having to scroll through duplicates.该值应该能够根据第二个工作表中 B 列的内容从下拉列表中选择,而且无需滚动重复项。
I am happy to use either VBA or general excel formulas to achieve this.我很高兴使用 VBA 或通用 excel 公式来实现这一点。 I am also aware that I don't think I have explained this very well so please feel free to prompt me for more information as required.我也知道我认为我没有很好地解释这一点,所以请随时根据需要提示我提供更多信息。
Here is a start.这是一个开始。 I assume the the list of items is in Sheet2 somewhere from B1 through B1000 .我假设项目列表位于从B1到B1000 的Sheet2 中。 It's OK if the range is only partially filled (adjust the 1000 to suit your needs) .如果范围只是部分填充(调整 1000 以满足您的需要),那也没关系。
The code scans this list and builds a DV string.代码扫描此列表并构建 DV 字符串。 The Data Validation is then applied to Sheet1 cell C5 :然后将数据验证应用于Sheet1单元格C5 :
Sub setupDV()
Dim rSource As Range, rDV As Range, r As Range, csString As String
Dim c As Collection
Set rSource = Sheets("Sheet2").Range("B1:B1000")
Set rDV = Sheets("Sheet1").Range("C5")
Set c = New Collection
csString = ""
On Error Resume Next
For Each r In rSource
v = r.Value
If v <> "" Then
c.Add v, CStr(v)
If Err.Number = 0 Then
If csString = "" Then
csString = v
Else
csString = csString & "," & v
End If
Else
Err.Number = 0
End If
End If
Next r
On Error GoTo 0
'MsgBox csString
With rDV.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=csString
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = False
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.