|
- Sub test1() '汇总 测试
- Dim Conn As Object, SQL As String, strConn As String, i As Long
- Dim A As String, B As String, C As String
-
- Set Conn = CreateObject("ADODB.Connection")
- If Application.Version < 12 Then
- strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source="
- Else
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source="
- End If
- Conn.Open strConn & ThisWorkbook.FullName
-
- Worksheets("汇总").Activate
- ActiveSheet.UsedRange.Offset(1).ClearContents
-
- A = "SELECT 品名,期初 FROM [期初$] WHERE 品名 IS NOT NULL"
- B = "SELECT 品名,SUM(数量) AS 入库总数量 FROM [入库$] WHERE 品名 IS NOT NULL GROUP BY 品名"
- C = "SELECT 品名,SUM(数量) AS 出库总数量,SUM(金额) AS 出库总金额 FROM [出库$] WHERE 品名 IS NOT NULL GROUP BY 品名"
-
- SQL = "SELECT a.品名 AS 名称,a.期初,b.入库总数量,c.出库总数量,c.出库总金额,b.入库总数量-c.出库总数量 AS 结余 FROM " & _
- "((" & A & ") a LEFT JOIN (" & B & ") b ON a.品名=b.品名 ) LEFT JOIN (" & C & ") c ON a.品名=c.品名"
- Range("A2").CopyFromRecordset Conn.Execute(SQL)
-
- Conn.Close
- Set Conn = Nothing
- Beep
- End Sub
- Sub test2() '拆分 测试
- Dim Conn As Object, rs As Object, SQL As String
- Dim wks As Worksheet, Fields_, pos As Long
- Dim A As String, B As String, C As String
- Dim strConn As String, str_ As String
-
- DoApp False
-
- For Each wks In Worksheets
- If wks.Index > 4 Then wks.Delete
- Next
-
- Set Conn = CreateObject("ADODB.Connection")
- Set rs = CreateObject("ADODB.Recordset")
- If Application.Version < 12 Then
- strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source="
- Else
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source="
- End If
- Conn.Open strConn & ThisWorkbook.FullName
-
- Fields_ = Split("商品名称 入货日期 入货数量 出货日期 出货数量 出库金额")
- A = "SELECT 品名,'期初',期初 FROM [期初$] WHERE 品名='[str_]'"
- B = "SELECT 品名 AS 商品名称,日期 AS 入货日期,数量 AS 入货数量 FROM [入库$] WHERE 品名='[str_]' ORDER BY 日期"
- C = "SELECT 日期 AS 出货日期,数量 AS 出货数量余,小计 AS 出库金额 FROM [出库$] WHERE 品名='[str_]' ORDER BY 日期"
-
- SQL = "SELECT DISTINCT 品名 FROM [入库$] WHERE 品名 IS NOT NULL"
- rs.Open SQL, Conn, 1, 3
-
- While Not rs.EOF
- str_ = rs.Fields(0).Value
- Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = str_
- With ActiveSheet
- .Range("B1") = "入库"
- .Range("E1") = "出库"
- .Range("B2").Resize(, UBound(Fields_) + 1) = Fields_
- .Range("B3").CopyFromRecordset Conn.Execute(Replace(A, "[str_]", str_))
- .Range("B4").CopyFromRecordset Conn.Execute(Replace(B, "[str_]", str_))
- .Range("E4").CopyFromRecordset Conn.Execute(Replace(C, "[str_]", str_))
- .Columns.AutoFit
- pos = .Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row + 1
- .Cells(pos, "B") = "小计"
- .Cells(pos, "D") = "=sum(r4c:r[-1]c)"
- .Cells(pos, "F") = "=sum(r4c:r[-1]c)"
- .Cells(pos, "G") = "=sum(r4c:r[-1]c)"
- With .Range("B1").CurrentRegion
- .Columns(2).NumberFormatLocal = "YYYY/M/D"
- .Columns(4).NumberFormatLocal = "YYYY/M/D"
- .Borders.LineStyle = xlContinuous
- .HorizontalAlignment = xlCenter
- End With
- .Range("B1").Resize(, 3).HorizontalAlignment = xlCenterAcrossSelection
- .Range("E1").Resize(, 3).HorizontalAlignment = xlCenterAcrossSelection
- End With
- rs.MoveNext
- Wend
-
- rs.Close
- Set rs = Nothing
- Conn.Close
- Set Conn = Nothing
-
- Worksheets("汇总").Activate
- DoApp
- Beep
- End Sub
- Function DoApp(Optional Flag As Boolean = True)
- With Application
- .ScreenUpdating = Flag
- .DisplayAlerts = Flag
- .Calculation = -Flag * 30 - 4135
- End With
- End Function
复制代码 |
|