|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
ADO+数组请参考:- Sub 宏1()
- Dim cnn As Object, SQL$, i&, j&, m&, t, arr, brr, crr(), MyPath$
- t = [a1]
- arr = Range("B1:B" & Range("B" & Rows.Count).End(xlUp).Row)
- ReDim crr(1 To UBound(arr), 1 To 1)
- MyPath = ThisWorkbook.Path & "\数据"
- For i = 1 To UBound(arr)
- f = MyPath & arr(i, 1) & ".xlsx"
- If Dir(f) <> "" Then
- m = m + 1
- If m = 1 Then
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='excel 12.0;hdr=no';Data Source=" & f
- SQL = "select * from [Sheet1$a:a]"
- Else
- SQL = "select * from [Excel 12.0;hdr=no;Database=" & f & ";].[Sheet1$a:a]"
- End If
- brr = cnn.Execute(SQL).GetRows
- For j = 0 To UBound(brr, 2)
- If brr(0, j) = t Then
- crr(i, 1) = "A" & j + 1
- Exit For
- End If
- Next
- End If
- Next
- [c1].Resize(i - 1) = crr
- cnn.Close
- Set cnn = Nothing
- End Sub
复制代码 |
|