我有一个表作为数据源。根据单元格的内容,我选择要包含在验证列表中的数据。在字符串增加到超过255个字符之前,它可以正常工作。我看到这是一个局限性,可以使用命名范围而不是逗号分隔的字符串,但是我不知道如何从VBA中做到这一点。我认为我应该做些类似的事情。或者也许我可以直接创建创建命名范围而不使用字符串。有想法吗?
请查看该函数的代码:
Function GetPOholder()
Dim ws, ws1 As Worksheet
'Dim Source_PO_Holder As Range ', people As Range
Dim item As Variant
Dim test As Variant
Dim list, division As String
'change Sheet1 to suit
Set ws = ThisWorkbook.Worksheets("lists")
Set ws1 = ThisWorkbook.Worksheets("Sheet1")
division = ws1.Range("Division").value
Debug.Print "Division: " & division
ws1.Range("ordered_by").Validation.Delete 'delete previous validation
For Each item In ws.Range("name_ordered_by")
test = Application.WorksheetFunction.Index(ws.Range("Source_people_div"), Application.WorksheetFunction.Match(item, ws.Range("name_ordered_by"), 0))
If test = division Then
list = list & "," & item
Debug.Print test & " " & item
End If
Next
list = Right(list, Len(list) - 1)
ws1.Range("ordered_by").Validation.Add Type:=xlValidateList, Formula1:=list
End Function