|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub Test()
- Dim strShName As String
- Dim shFolder As Worksheet, shDoc As Worksheet, objList As Object
- Dim arr As Variant
- Dim lngRow As Long
- Dim strID As String, strList As String, strSplit() As String
- Dim arrKeys As Variant, arrItems As Variant
- Dim Rg As Range
-
- strShName = "FoldersGet"
- Set shFolder = Sheets(strShName)
- Set shDoc = Sheets("Documents")
-
- arr = shFolder.UsedRange
- Set objList = CreateObject("Scripting.Dictionary")
-
- For lngRow = 2 To UBound(arr)
- strID = Trim(arr(lngRow, 5))
- strList = arr(lngRow, 7)
- objList(strID) = objList(strID) & strList & "|"
- Next
-
- arrKeys = objList.keys
- arrItems = objList.items
-
- Set Rg = shFolder.Range("O1") '将O列当作来源列
- For lngRow = LBound(arrItems) To UBound(arrItems)
- strSplit = Split(arrItems(lngRow), "|")
- Rg.Resize(UBound(strSplit), 1) = Application.WorksheetFunction.Transpose(strSplit)
- strID = arrKeys(lngRow)
- strList = "=" & strShName & "!" & Rg.Resize(UBound(strSplit), 1).Address
- Set Rg = Rg.Offset(UBound(strSplit), 0)
- objList(strID) = strList
- Next
-
- lngRow = shDoc.Range("C" & Rows.Count).End(xlUp).Row
- arr = shDoc.Range("C1:C" & lngRow)
-
- For lngRow = 2 To UBound(arr)
- strID = Trim(arr(lngRow, 1))
- If strID <> "" Then strList = objList(strID)
- With shDoc.Cells(lngRow, 4).Validation
- .Delete
- .Add Type:=xlValidateList, Formula1:=strList
- End With
- Next
-
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|