ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助]用VBA编写SQL查询问题

[复制链接]

TA的精华主题

TA的得分主题

发表于 2006-3-27 15:26 | 显示全部楼层 |阅读模式
多条件求和在函数公式中。多用SUMPRODUCT. 但现在由于数据明细比较大,用公式,非常慢! 请教各位用VBA怎么实现? aqKJZVPj.rar (10.09 KB, 下载次数: 29)

TA的精华主题

TA的得分主题

发表于 2006-3-27 17:08 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
用数据透视表比较简单 qkW42NJL.rar (11.48 KB, 下载次数: 26)

TA的精华主题

TA的得分主题

 楼主| 发表于 2006-3-28 08:33 | 显示全部楼层

谢谢,兰色幻想兄,

可你做的透视表是达不到我想要的要求的。

我现在用的是公式,基本可以用,就是速度慢!

我想用vba代码来解决,

请各位出点意见!?

TA的精华主题

TA的得分主题

发表于 2006-3-28 09:10 | 显示全部楼层
要求说得不清楚,不知道你想实现什么样子。至少你应该先把结果(手工)填出来,否则不好帮你。

TA的精华主题

TA的得分主题

发表于 2006-3-28 10:52 | 显示全部楼层
试了一下,看如何? 8HUP7geZ.rar (18.33 KB, 下载次数: 83)

TA的精华主题

TA的得分主题

 楼主| 发表于 2006-3-28 11:38 | 显示全部楼层

谢谢

爱歌学习!

我先研究一下!

UNARTHUR,可能你不是做财务的吧,我是没有怎么说清楚,但只要是做财务的一眼看到就知道我的要求了 !呵呵!~

TA的精华主题

TA的得分主题

 楼主| 发表于 2006-3-30 08:48 | 显示全部楼层

请教爱歌学习,我想 按品种表A列的品号来做关键字,意思是品种表A列给定的品号不需要排序

请问怎么做?

TA的精华主题

TA的得分主题

发表于 2006-3-31 12:04 | 显示全部楼层

爱歌学习,传的实例好,要是能加上说明,就更好了,

偶是初学者,有些看不懂....

TA的精华主题

TA的得分主题

发表于 2006-3-31 13:59 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
Sub total() Dim Sql$, line&, i& Application.ScreenUpdating = False Set xx = CreateObject("adodb.connection") With xx .Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source=" & ThisWorkbook.FullName Sql = "select 品号,品名,sum(数量),sum(金额),类型 from [明细$] where month(日期)=" & Month(ActiveSheet.[j2]) & " group by 品号,品名,类型 order by 品号,类型 desc" [a5].CopyFromRecordset .Execute(Sql) Sql = "select 品号,sum(数量),sum(金额),类型 from [明细$] group by 品号,品名,类型 order by 品号,类型 desc" [k5].CopyFromRecordset .Execute(Sql) For i = 5 To [e65536].End(xlUp).Row If Cells(i, 5) = "退货" Then '将退货记录移到右边 If Cells(i, 1) <> Cells(i - 1, 1) Then Range(Cells(i, 3), Cells(i, 4)).Copy Destination:=Cells(i, 7) Range(Cells(i, 3), Cells(i, 4)).Clear Else Range(Cells(i, 3), Cells(i, 4)).Copy Destination:=Cells(i - 1, 7) Range(Cells(i, 1), Cells(i, 5)).Delete i = i - 1 End If End If Next For i = 5 To [e65536].End(xlUp).Row If Cells(i, 14) = "销售" Then '将累计销售记录原样复制到指定位置 Range(Cells(i, 12), Cells(i, 13)).Copy Destination:=Cells(i, 5) Else If Cells(i, 11) <> Cells(i - 1, 11) Then '将退货记录移到右边 Range(Cells(i, 12), Cells(i, 13)).Copy Destination:=Cells(i, 9) Cells(i, 5).Clear Else Range(Cells(i, 12), Cells(i, 13)).Copy Destination:=Cells(i - 1, 9) Range(Cells(i, 11), Cells(i, 14)).Delete i = i - 1 End If End If Next Range(Cells(5, 11), Cells(65536, 14)).Clear '清除临时数据 line = [a65536].End(xlUp).Row Range(Cells(line + 1, 1), Cells(65536, 10)).Clear Range(Cells(5, 1), Cells(line, 10)).Select '下面是录制的宏用来给整个数据区域加框线 Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With .Close Set xx = Nothing Application.ScreenUpdating = True End With End Sub 其中 Sql = "select 品号,品名,sum(数量),sum(金额),类型 from [明细$] where month(日期)=" & Month(ActiveSheet.[j2]) & " group by 品号,品名,类型 order by 品号,类型 desc" 蓝色部分是根据 J2 的月份查询当月数据 红色部分是根据 品号,品名,类型 分类汇总 绿色部分是按 品号,类型 降序排序 分两次把当月和累积的数据筛选出来后,由于进货退货都在同一列上,所以下面的工作就是将退货记录移到要求的位置,并从进货部分删除。 你把“清除临时数据”那一句注解掉,就可以看到第一次筛选出来的数据排列情况。

TA的精华主题

TA的得分主题

 楼主| 发表于 2006-3-31 22:42 | 显示全部楼层

谢谢!

我暂时能明白些.但还没有深刻领会.

研究中......

请问爱歌学习,你学习这些ADO,SQL有什么心得?!

怎么你就写的这么溜呢!能否给大家说说?!

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-5-9 21:03 , Processed in 0.041441 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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