|
- Sub ADO加数组() '不能保留格式
- Dim cnn As Object, rs As Object, rst As Object, s$, MyPath$, MyFile$, m&, sh As Worksheet
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- For Each sh In Worksheets
- If sh.Name <> "封面" Then sh.Delete
- Next
- MyPath = ThisWorkbook.Path & ""
- MyFile = Dir(MyPath & "*.xls")
- Do While MyFile <> ""
- If InStr(MyFile, ThisWorkbook.Name) = 0 Then
- Set cnn = CreateObject("ADODB.Connection")
-
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;" _
- & "Data Source=" & MyPath & MyFile
-
- 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
- Set rst = cnn.Execute("[" & s & "]")
- m = m + 1
- With Sheets.Add(After:=Sheets(m))
- .Name = Right(Replace(MyFile, ".xls", ""), 5) & "-" & Left(s, Len(s) - 1)
- For i = 1 To rst.Fields.Count
- .Cells(1, i) = rst.Fields(i - 1).Name
- Next
- .[a2].CopyFromRecordset rst
- End With
- End If
- End If
- rs.MoveNext
- Loop
- End If
- MyFile = Dir()
- Loop
- rs.Close
- rst.Close
- cnn.Close
- Set rs = Nothing
- Set rst = Nothing
- Set cnn = Nothing
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|