Office中国论坛/Access中国论坛
标题:
函数真的卡 求老师用vba 写段代码
[打印本页]
作者:
yefeng1019
时间:
2019-12-7 08:53
标题:
函数真的卡 求老师用vba 写段代码
用函数统计数量 多条件求和
数据一多真的很卡 请老师帮忙用vba写段代码
万分感谢
作者:
yefeng1019
时间:
2019-12-7 09:11
自己顶一下 不要沉了
作者:
roych
时间:
2019-12-9 10:45
看了下,这个鬼大概要写ado,晚上再说吧。
Function getRst(ByVal strWhere As String) As Dictionary
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim dict As New Dictionary
Dim strSQL As String
Application.ScreenUpdating = False
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='excel 12.0;hdr=yes';Data Source=" & ThisWorkbook.FullName
strSQL = "select 科目,sum(实际收费) as 收费合计 from [学员档案汇总$A2:AI699] where 季度='" & strWhere & "' group by 科目"
rst.Open strSQL, cnn, adOpenKeyset, adLockOptimistic
Do Until rst.EOF
dict.Add rst(0).Value, rst(1).Value
rst.MoveNext
Loop
Set getRst = dict
Set dict = Nothing
rst.Close
cnn.Close
End Function
Sub test()
Dim dict As New Dictionary
Set dict = getRst("19秋季")
Sheets("科目数据").Range("C6").Resize(dict.Count, 1) = Application.Transpose(dict.Keys)
Sheets("科目数据").Range("J6").Resize(dict.Count, 1) = Application.Transpose(dict.Items)
End Sub
复制代码
[attach]63573[/attach]
其它的写法类似,这里就不再写了。这可能需要你花点时间去百度一下SQL语句。
欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/)
Powered by Discuz! X3.3