|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Private Sub CommandButton1_Click()
Set cnn = CreateObject("adodb.connection")
cnn.Open "provider=microsoft.jet.oledb.4.0;data source=" & ThisWorkbook.Path & "\数据库.mdb"
rr = [f5]
For i = 6 To 25
SQL = "select 月份,null,sum(数量) as 数量,sum(金额) as 金额 from 数据库 where 产品名称='" & [b2] & "' and 月份=" & Cells(i, 1) & " group by 月份"
If IsNumeric(Cells(i, 1)) Then
Cells(i, 1).CopyFromRecordset cnn.Execute(SQL)
rr = rr + Cells(i, 4): Cells(i, 6) = rr: rr1 = rr1 + Cells(i, 3): rr3 = rr3 + Cells(i, 4)
End If
If Cells(i, 1) = "季度合计" And Cells(i - 1, 3) <> "" Then
Cells(i, 3) = rr1: rr2 = rr1 + rr2: rr1 = 0: Cells(i, 4) = rr3: rr3 = 0
End If
If Cells(i, 1) = "本年累计" And Cells(i - 2, 3) <> "" Then
Cells(i, 3) = rr2: Cells(i, 4) = rr - [f5]
End If
Next
cnn.Close: Set cnn = Nothing
End Sub
|
|