|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 fsc163198 于 2011-12-1 20:40 编辑
- Sub 按月汇总()
- Set conn = New ADODB.Connection
- conn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=Excel 8.0;Data Source=" & ThisWorkbook.FullName
- conn.Open
- If conn.State = adStateOpen Then
- sSql = "Select distinct 客户 From [销售记录$A1:C65536] where 合计 > 0"
- Cells(2, 1).CopyFromRecordset conn.Execute(sSql)
- conn.Close
- End If
- Set conn = Nothing
- For i = 1 To 12
- Dim AdoCn As New ADODB.Connection, Rst As New ADODB.Recordset
- AdoCn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=Yes;';Data Source=" & ThisWorkbook.FullName
- Rst.Open "Select Cnt From [月份汇总$a1:a65536] A Left Join (Select 客户,Sum(合计) As Cnt From [销售记录$A1:C65536] where Month(日期)= " & i & " Group By 客户) B On a.[客户]=b.[客户]", AdoCn
- Cells(2, i + 1).CopyFromRecordset Rst
- Rst.Close: AdoCn.Close: Set Rst = Nothing: Set AdoCn = Nothing
- Next i
- End Sub
复制代码
按月份汇总.rar
(9.76 KB, 下载次数: 132)
我这个是用两条SQL语句拼起来的结果是出来了 但我感觉应该有更好的方法 请高手指点
|
|