ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 3042|回复: 15

[讨论] 月度报表用SQL如何进行简化

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-8-6 13:42 | 显示全部楼层 |阅读模式
方法是达到了,但有点繁琐,望大侠指点,谢谢1
Sub MonthFind()
    Dim cnn As Object, Sql As String
    Dim iRow As Integer
    Set cnn = CreateObject("adodb.connection")
        cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=yes;imex=1';data source=" & ThisWorkbook.FullName
    With Sheet4
        .[a6:i65536].ClearContents
        Sql = "select [件号],[名称],[计划价],sum(数量),sum(金额),0,0,0,0 from [入库登记$a5:l65536] where month(入库日期) < '" & Month(Sheet3.[j4]) & "' group by 件号,名称,计划价"
        Sql = "select * from (" & Sql & ") where [件号] is not null"
            .Range("a6").CopyFromRecordset cnn.Execute(Sql)
        Sql = "select [件号],[名称],[计划价],-sum(数量),-sum(金额),0,0,0,0 from [出库登记$a5:n65536] where month(出库日期) < '" & Month(Sheet3.[j4]) & "' group by 件号,名称,计划价"
            iRow = .[a65536].End(xlUp).Row + 1
        Sql = "select * from (" & Sql & ") where [件号] is not null"
            .Range("a" & iRow).CopyFromRecordset cnn.Execute(Sql)
        Sql = "select [件号],[名称],[计划价],0,0,sum(数量),sum(金额),0,0 from [入库登记$a5:l65536] where month(入库日期) = '" & Month(Sheet3.[j4]) & "' group by 件号,名称,计划价"
            iRow = .[a65536].End(xlUp).Row + 1
        Sql = "select * from (" & Sql & ") where [件号] is not null"
            .Range("a" & iRow).CopyFromRecordset cnn.Execute(Sql)
        Sql = "select [件号],[名称],[计划价],0,0,0,0,sum(数量),sum(金额) from [出库登记$a5:n65536] where month(出库日期) = '" & Month(Sheet3.[j4]) & "' group by 件号,名称,计划价"
            iRow = .[a65536].End(xlUp).Row + 1
        Sql = "select * from (" & Sql & ") where [件号] is not null"
            .Range("a" & iRow).CopyFromRecordset cnn.Execute(Sql)
            End With
    With Sheet3
            .[a6:i65536].ClearContents
        Sql = "select [件号],[名称],[计划价],sum(期初库存),sum(期初金额),sum(本期入库),sum(入库金额),sum(本期出库),sum(出库金额) from [Temp$a5:i65536]where [件号] is not null  group by 件号,名称,计划价"
            .Range("a6").CopyFromRecordset cnn.Execute(Sql)
    End With
End Sub

月度报表.rar

63.22 KB, 下载次数: 26

TA的精华主题

TA的得分主题

发表于 2018-8-6 16:33 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
  1. Sub MonthFind()
  2.     Dim cnn As Object, Sql As String, yf
  3.     Set cnn = CreateObject("adodb.connection")
  4.     cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=yes;imex=1';data source=" & ThisWorkbook.FullName
  5.     yf = Month(Sheet3.[j4])
  6.     With Sheet3
  7.         .[a6:i65536].ClearContents
  8.         Sql = "select [件号],[名称],[计划价],sum(数量) as d,sum(金额) as e,0 as f,0 as g,0 as h ,0 as i from [入库登记$a5:l65536] where month(入库日期) < '" & yf & "' group by 件号,名称,计划价 union all " _
  9.            & "select [件号],[名称],[计划价],-sum(数量) as d,-sum(金额) as e,0 as f,0 as g,0 as h ,0 as i from [出库登记$a5:n65536] where month(出库日期) < '" & yf & "' group by 件号,名称,计划价 union all " _
  10.            & "select [件号],[名称],[计划价],0 as d,0 as e,sum(数量) as f,sum(金额) as g,0 as h,0 as i  from [入库登记$a5:l65536] where month(入库日期) = '" & yf & "' group by 件号,名称,计划价  union all " _
  11.            & "select [件号],[名称],[计划价],0 as d,0 as e,0 as f ,0 as g,sum(数量) as h ,sum(金额) as i from [出库登记$a5:n65536] where month(出库日期) = '" & yf & "' group by 件号,名称,计划价"
  12.        Sql = "select [件号],[名称],[计划价],sum(d),sum(e),sum(f),sum(g),sum(h),sum(i) from (" & Sql & ") where [件号] is not null  group by 件号,名称,计划价"
  13.        .[a6].CopyFromRecordset cnn.Execute(Sql)
  14.     End With
  15. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-8-6 23:21 | 显示全部楼层

太酷,太厉害了,我试几次都没成功,一直报错。谢谢!

TA的精华主题

TA的得分主题

发表于 2018-8-7 14:31 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
没报错啊,哪里错了?

月度报表.rar

60.65 KB, 下载次数: 37

TA的精华主题

TA的得分主题

发表于 2018-8-7 15:13 | 显示全部楼层
还可以简化一点。
  1. Sub MonthFind()
  2.     Dim cnn As Object, Sql As String, yf%, tj1$, tj2$
  3.     Set cnn = CreateObject("adodb.connection")
  4.     cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=yes;imex=1';data source=" & ThisWorkbook.FullName
  5.     yf = Month(Sheet3.[j4])
  6.     tj1 = "(iif(month(入库日期)<" & yf & ",1,0))"
  7.     tj2 = "(iif(month(出库日期)<" & yf & ",1,0))"
  8.     With Sheet3
  9.        .[a6:i65536].ClearContents
  10.        Sql = "select 件号,名称,计划价,数量*" & tj1 & " as d,金额*" & tj1 & " as e,数量*(1-" & tj1 & ") as f,金额*(1-" & tj1 & ") as g,0 as h,0 as i from [入库登记$a5:l] union all " _
  11.            & "select 件号,名称,计划价,-数量*" & tj2 & " as d,-金额*" & tj2 & " as e,0 as f,0 as g,数量*(1-" & tj2 & ") as h,金额*(1-" & tj2 & ") as i from [出库登记$a5:n]"
  12.       Sql = "select 件号,名称,计划价,sum(d),sum(e),sum(f),sum(g),sum(h),sum(i) from (" & Sql & ") where 件号 is not null  group by 件号,名称,计划价"
  13.        .[a6].CopyFromRecordset cnn.Execute(Sql)
  14.     End With
  15. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-10-9 18:38 | 显示全部楼层
grf1973 发表于 2018-8-7 15:13
还可以简化一点。

太厉害了,谢谢!给您点赞!

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-10-9 18:39 | 显示全部楼层
grf1973 发表于 2018-8-7 14:31
没报错啊,哪里错了?

没有错,是我自己改了几回代码,均出错,呵呵

TA的精华主题

TA的得分主题

发表于 2018-10-11 11:05 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册

大师好!如果不是按月度查询 而是按直接按日期来查询,应该怎么来表达,不会sql,请指教,谢谢!
复制.png

TA的精华主题

TA的得分主题

发表于 2018-10-11 11:28 | 显示全部楼层
yf写成  "# " & sheet3.[j4] & " #"
日期比较用 入库日期 < & yf & ...... 试试

TA的精华主题

TA的得分主题

发表于 2018-10-11 11:53 | 显示全部楼层
grf1973 发表于 2018-10-11 11:28
yf写成  "# " & sheet3.[j4] & " #"
日期比较用 入库日期 < & yf & ...... 试试
还是有以下报错!不知道什么原因?
  1. Sub MonthFind()
  2.     Dim cnn As Object, Sql As String, yf, i&
  3.     Set cnn = CreateObject("adodb.connection")
  4.     cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=yes;imex=1';data source=" & ThisWorkbook.FullName
  5.     yf = "# " & Sheet3.[j4] & " #"
  6.     With Sheet3
  7.         .[a6:i65536].ClearContents
  8.         Sql = "select [件号],[名称],[计划价],sum(数量) as d,sum(金额) as e,0 as f,0 as g,0 as h ,0 as i from [入库登记$a5:l65536] where 入库日期 < & yf &  group by 件号,名称,计划价 union all " _
  9.            & "select [件号],[名称],[计划价],-sum(数量) as d,-sum(金额) as e,0 as f,0 as g,0 as h ,0 as i from [出库登记$a5:n65536] where 出库日期 <  & yf &  group by 件号,名称,计划价 union all " _
  10.            & "select [件号],[名称],[计划价],0 as d,0 as e,sum(数量) as f,sum(金额) as g,0 as h,0 as i  from [入库登记$a5:l65536] where 入库日期 =  & yf &  group by 件号,名称,计划价  union all " _
  11.            & "select [件号],[名称],[计划价],0 as d,0 as e,0 as f ,0 as g,sum(数量) as h ,sum(金额) as i from [出库登记$a5:n65536] where 出库日期 =  & yf &  group by 件号,名称,计划价"
  12.        Sql = "select [件号],[名称],[计划价],sum(d),sum(e),sum(f),sum(g),sum(h),sum(i) from (" & Sql & ") where [件号] is not null  group by 件号,名称,计划价"
  13.        .[a6].CopyFromRecordset cnn.Execute(Sql)
  14.    For i = 6 To .[a65536].End(xlUp).Row
  15.        .Cells(i, "j") = .Cells(i, "d") + .Cells(i, "f") - .Cells(i, "h")
  16.       .Cells(i, "k") = .Cells(i, "e") + .Cells(i, "g") - .Cells(i, "i")
  17.        Next
  18.   End With
  19. End Sub


复制代码
12.png
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-26 01:20 , Processed in 0.039571 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表