|
nshk0898 发表于 2013-3-14 11:08
非常感谢,基本能够实现我要的效果.您用的这种方法实在不懂,所以恳请能否再改下让查找的范围是整个“工作 ... - Sub 查找整个工作簿()
- Dim cnn As Object, rs As Object, SQL$, s$, n%
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "provider=Microsoft.ACE.OLEDB.12.0;extended properties='excel 12.0;hdr=no';data source=" & ThisWorkbook.Path & "\456.xlsx"
- Set rs = cnn.OpenSchema(20)
- Do Until rs.EOF
- If rs.Fields("TABLE_TYPE") = "TABLE" Then
- s = Replace(rs("TABLE_NAME").Value, "'", "")
- If Right(s, 1) = "$" Then
- n = n + 1
- If n = 1 Then
- SQL = "select * from [" & s & "]"
- Else
- SQL = SQL & " union all select * from [" & s & "]"
- End If
- End If
- End If
- rs.MoveNext
- Loop
- SQL = "select iif(isnull(b.f1),'没有找到',b.f1),f2,f3 from [Excel 12.0;hdr=no;Database=" & ThisWorkbook.FullName & "].[Sheet1$A2:A" & Cells(Rows.Count, 1).End(xlUp).Row & "] a left join (" & SQL & ") b on instr(b.f3 ,a.f1)>0"
- [c2].CopyFromRecordset cnn.Execute(SQL)
- rs.Close
- Set rs = Nothing
- cnn.Close
- Set cnn = Nothing
- End Sub
复制代码 |
|