|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Dim strPath As String
- ' With Application.FileDialog(msoFileDialogFolderPicker)
- ' .InitialFileName = ThisWorkbook.Path
- ' If .Show Then strPath = .SelectedItems(1) Else: Exit Sub
- ' End With
- ' If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
- strPath = ThisWorkbook.Path & "\文件夹\"
-
- Dim Conn As Object, Cata As Object, tb As Object
- Dim strConn As String, strFile As String, t As String, i As Long
-
- Sheet1.Activate
- ActiveSheet.UsedRange.Offset(1).ClearContents
-
- Application.ScreenUpdating = False
-
- Set Conn = CreateObject("ADODB.Connection")
- Set Cata = CreateObject("ADOX.Catalog")
-
- If Application.Version < 12 Then
- strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=NO';Data Source="
- Else
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=NO';Data Source="
- End If
- 'Conn.Open strConn & ThisWorkbook.FullName
-
- On Error Resume Next '数据不规范,无奈之举,用上容错……
- strFile = Dir(strPath & "*.xls*")
- While Len(strFile)
- If strPath & strFile <> ThisWorkbook.FullName Then
- Conn.Open strConn & strPath & strFile
- Cata.ActiveConnection = Conn
- For Each tb In Cata.Tables
- If tb.Type = "TABLE" Then
- t = Replace(tb.Name, "'", vbNullString)
- If Right(t, 1) = "$" Then
- i = i + 1
- With Range("A1")
- .Offset(i, 0).CopyFromRecordset Conn.Execute("SELECT * FROM [" & t & "D10:D10]")
- .Offset(i, 1).CopyFromRecordset Conn.Execute("SELECT * FROM [大大]")
- .Offset(i, 2).CopyFromRecordset Conn.Execute("SELECT * FROM [小小]")
- .Offset(i, 3) = Split(strFile, ".xls")(0)
- .Offset(i, 4) = Left(t, Len(t) - 1)
- End With
- End If
- If Err.Number Then Err.Clear
- End If
- Next
- If Conn.State = 1 Then Conn.Close
- End If
- strFile = Dir
- Wend
-
- Set Cata = Nothing
- Set Conn = Nothing
-
- Application.ScreenUpdating = True
- Beep
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|