|
Sub 去重汇总() '2024/05/19
Dim Sql$, line&, i&
Application.ScreenUpdating = False
Set cnn = CreateObject("adodb.connection")
Set rst = CreateObject("ADODB.RecordSet")
With cnn
.Open "Provider=Microsoft.Ace.oledb.12.0;extended properties='excel 12.0;HDR=yes';data source=" & ThisWorkbook.FullName
Sql = "select 日期,品名,类型,sum(数量),sum(金额) from [明细$] group by 日期,品名,类型 order by 日期,品名,类型 desc"
End With
Sheet3.Range("a5:f10000").ClearContents
arr = Array("日期", "品名", "进货", "销售")
Sheet3.Range("b5").Resize(1, 4) = arr
rst.Open Sql, cnn, 1, 3
Do While Not rst.EOF
If s <> rst.Fields(0) & rst.Fields(1) Then
s = rst.Fields(0) & rst.Fields(1)
k = Sheet3.Cells(Rows.Count, 2).End(xlUp).Row + 1
Sheet3.Cells(k, 2).Value = rst.Fields(0)
Sheet3.Cells(k, 3).Value = rst.Fields(1)
If rst.Fields(2) = "进货" Then
Sheet3.Cells(k, 4).Value = rst.Fields(4)
Sheet3.Cells(k, 5).Value = 0
ElseIf rst.Fields(2) = "销售" Then
Sheet3.Cells(k, 5).Value = rst.Fields(4)
Sheet3.Cells(k, 4).Value = 0
End If
Else
m = Sheet3.Cells(Rows.Count, 2).End(xlUp).Row
If rst.Fields(2) = "进货" Then
Sheet3.Cells(m, 4).Value = rst.Fields(4)
ElseIf rst.Fields(2) = "销售" Then
Sheet3.Cells(m, 5).Value = rst.Fields(4)
End If
End If
rst.MoveNext
Loop
Application.ScreenUpdating = True
MsgBox "处理完毕!"
End Sub |
|