|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
张雄友 发表于 2014-12-15 18:39
如果A列内容在多个工作表出现过怎么办?
多个工作簿吧?如果是用逗号隔开:- Sub ADO加字典()
- Dim cnn As Object, rs As Object, rst As Object, SQL$, Mypath$, MyFile$, s$
- Dim d As Object, arr, brr$(), i&, fn$
- Set d = CreateObject("scripting.dictionary")
- Mypath = ThisWorkbook.Path & "\我的文件夹"
- MyFile = Dir(Mypath & "*.xlsx")
- Do While MyFile <> ""
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "Provider=Microsoft.ace.OLEDB.12.0;Extended Properties='Excel 12.0;hdr=no';Data Source=" & Mypath & MyFile
- Set rs = cnn.OpenSchema(20)
- fn = Replace(MyFile, ".xlsx", "")
- Do Until rs.EOF
- If rs.Fields("TABLE_TYPE") = "TABLE" Then
- s = Replace(rs("TABLE_NAME").Value, "'", "")
- If Right(s, 1) = "$" Then
- SQL = "select f1 from [" & s & "] where f1 is not null"
- Set rst = cnn.Execute(SQL)
- If Not rst.EOF Then
- arr = rst.GetRows
- For i = 0 To UBound(arr, 2)
- If Not d.Exists(arr(0, i)) Then d(arr(0, i)) = fn Else d(arr(0, i)) = d(arr(0, i)) & "," & fn
- Next
- End If
- End If
- End If
- rs.MoveNext
- Loop
- MyFile = Dir()
- Loop
- arr = Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
- ReDim brr(1 To UBound(arr), 1 To 1)
- For i = 1 To UBound(arr)
- brr(i, 1) = d(arr(i, 1))
- Next
- [b2].Resize(i - 1) = brr
- rs.Close
- rst.Close
- Set rs = Nothing
- Set rst = Nothing
- cnn.Close
- Set cnn = Nothing
- End Sub
复制代码 |
|