|
- Sub test1() '参与。
- Dim Conn As Object, rs As Object, Dict As Object
- Dim p As String, f As String, s As String
- Dim strConn As String, SQL As String, i As Integer
-
- Range("A1").CurrentRegion.ClearContents
- Application.ScreenUpdating = False
- Set Dict = CreateObject("Scripting.Dictionary")
- Set Conn = CreateObject("ADODB.Connection")
-
- s = "Excel 12.0;HDR=yes;IMEX=1;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?")
- SQL = "SELECT '[MD]' AS 日期,师傅 AS 名单,班号 FROM [" & s & p & "[f]].[$A2:C] WHERE LEN(师傅) UNION ALL " & _
- "SELECT '[MD]' AS 日期,学徒 AS 名单,班号 FROM [" & s & p & "[f]].[$A2:C] WHERE LEN(学徒)"
- Do
- If p & f <> ThisWorkbook.FullName Then
- Dict.Add Replace(Replace(SQL, "[MD]", Split(f, ".xls")(0)), "[f]", f), vbNullString
- End If
- f = Dir
- Loop While Len(f)
-
- If Dict.Count Then
- SQL = "TRANSFORM MAX(班号) SELECT 名单 FROM (" & Join(Dict.Keys, " UNION ALL ") & ") GROUP BY 名单 PIVOT 日期"
- Set rs = Conn.Execute(SQL)
- With Range("A1")
- For i = 0 To rs.Fields.Count - 1
- .Offset(0, i) = rs.Fields(i).Name
- Next
- .Offset(1).CopyFromRecordset rs
- .CurrentRegion.SpecialCells(xlCellTypeBlanks).Value = "休息"
- End With
- End If
-
- Set rs = Nothing
- Conn.Close
- Set Conn = Nothing
- Set Dict = Nothing
- Application.ScreenUpdating = True
- Beep
- End Sub
复制代码 |
|