|
- Sub test1() '练习
-
- Dim Conn As Object, rs As Object, Cata As Object, tb As Object, Dic As Object, Dict As Object
- Dim p As String, f As String, strConn As String, SQL As String, Field As String, s As String, t As String
- Dim ar, i As Long, vrtKey, wks As Worksheet
-
- Set Dic = CreateObject("Scripting.Dictionary")
-
- For Each wks In Worksheets
- With wks
- .Cells.ClearContents
- .UsedRange.Borders.LineStyle = xlNone
- Set Dic(.Name & "$") = CreateObject("Scripting.Dictionary")
- End With
- Next
-
- Application.ScreenUpdating = False
-
- Set Dict = CreateObject("Scripting.Dictionary")
- Set Conn = CreateObject("ADODB.Connection")
- Set rs = CreateObject("ADODB.Recordset")
- Set Cata = CreateObject("ADOX.Catalog")
-
- s = "Excel 12.0;HDR=YES;Database="
- If Application.Version < 12 Then
- s = Replace(s, "12.0", "8.0")
- strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=YES';Data Source="
- Else
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=YES';Data Source="
- End If
- Conn.Open strConn & ThisWorkbook.FullName
-
- p = ThisWorkbook.Path & "\"
- f = Dir(p & "*.xls*")
- Do
- If p & f <> ThisWorkbook.FullName Then
- Cata.ActiveConnection = strConn & p & f
- For Each tb In Cata.Tables
- If tb.Type = "TABLE" Then
- t = Replace(tb.Name, "'", vbNullString)
- If Right(t, 1) = "$" Then
- SQL = "SELECT * FROM [" & s & p & f & "].[" & t & "A1:Z1] WHERE FALSE"
- Set rs = Conn.Execute(SQL)
- For i = 0 To rs.Fields.Count - 1
- Field = rs.Fields(i).Name
- If Not Field Like "F[1-9]*" Then
- Field = "`" & Field & "`"
- If Not Dic(t).Exists(Field) Then Dic(t).Add Field, Dic(t).Count
- End If
- Next
- End If
- End If
- Next
- End If
- f = Dir
- Loop While f <> ""
-
- f = Dir(p & "*.xls*")
- Do
- If p & f <> ThisWorkbook.FullName Then
- Cata.ActiveConnection = strConn & p & f
- For Each tb In Cata.Tables
- If tb.Type = "TABLE" Then
- t = Replace(tb.Name, "'", vbNullString)
- If Right(t, 1) = "$" Then
- ar = Dic(t).Keys
- For i = 0 To UBound(ar)
- ar(i) = "NULL AS " & ar(i)
- Next
- SQL = "SELECT * FROM [" & s & p & f & "].[" & t & "A1:Z1] WHERE FALSE"
- Set rs = Conn.Execute(SQL)
- For i = 0 To rs.Fields.Count - 1
- Field = "`" & rs.Fields(i).Name & "`"
- ar(Dic(t)(Field)) = Field
- Next
- SQL = " UNION ALL SELECT " & Join(ar, ",") & " FROM [" & s & p & f & "].[" & t & "A1:Z] WHERE 序号 IS NOT NULL"
- Dict(t) = Dict(t) & SQL
- End If
- End If
- Next
- End If
- f = Dir
- Loop While f <> ""
- Set Cata = Nothing
-
- For Each vrtKey In Dict.Keys
- If rs.State = 1 Then rs.Close
- rs.Open Mid(Dict(vrtKey), 12), Conn, 1, 3
- With Worksheets(Replace(vrtKey, "$", vbNullString)).Range("A1")
- For i = 0 To rs.Fields.Count - 1
- .Offset(0, i) = rs.Fields(i).Name
- Next
- .Offset(1).CopyFromRecordset rs
- With .CurrentRegion
- .Borders.LineStyle = xlContinuous
- ar = .Columns(1)
- For i = 2 To UBound(ar)
- ar(i, 1) = i - 1
- Next
- .Columns(1) = ar
- End With
- End With
- Next
-
- If rs.State = 1 Then rs.Close
- Set rs = Nothing
- Conn.Close
- Set Conn = Nothing
- Set Dic = Nothing
- Set Dict = Nothing
-
- Application.ScreenUpdating = True
- Beep
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|