|
Sub test1()
Dim Conn As Object, rs As Object
Dim p As String, f As String, s As String
p = ThisWorkbook.Path & "\分簿\"
If Dir(p, vbDirectory) = "" Then MkDir p
Set Conn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0 XML;Data Source=" & ThisWorkbook.FullName
s = "SELECT DISTINCT FORMAT(送货日期,'YYYY年MM月') FROM [" & ActiveSheet.Name & "$] WHERE LEN(送货日期)"
rs.Open s, Conn, 1, 3
While Not rs.EOF
s = rs.Fields(0).Value
f = p & s & ".xlsx"
If Dir(f) <> "" Then Kill f
Conn.Execute "SELECT 客户名称,SUM(应收金额) AS 金额 INTO [" & f & "].[" & s & "] FROM [" & ActiveSheet.Name & "$] WHERE FORMAT(送货日期,'YYYY年MM月')='" & s & "' GROUP BY 客户名称"
rs.MoveNext
Wend
rs.Close
Set rs = Nothing
Conn.Close
Set Conn = Nothing
Beep
End Sub |
|