|
楼主 |
发表于 2018-8-16 14:10
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
没人回复,已自己搞定了。跟附件的表格不是一样的,复制了自己工作用的表格里写的代码。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Column <> 6 Or Target.Row < 3 Then Exit Sub '说明是哪列哪行开始需要设置数据有效性
Dim cnn As Object
Dim SQL As String, i&, aa
Set cnn = CreateObject("adodb.connection")
cnn.Open "provider=microsoft.Ace.oledb.12.0;extended properties=excel 12.0;data source=" & ThisWorkbook.Path & "\项目里程碑2018.xlsx"
SQL = "Select * from [项目里程碑$]"
'通过数据库链接到数据有效性序列要引用的表格
arr = cnn.Execute(SQL).GetRows
For i = 0 To UBound(arr, 2)
If arr(8, i) Like "*张三*" Then
aa = aa & arr(4, i) & ","
End If
Next
aa = Left(aa, Len(aa) - 1)
cnn.Close
Set cnn = Nothing
With Target.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=aa
End With
End Sub
|
|