|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
原帖由 望江婷 于 2011-3-8 14:47 发表
请教各位老师能做到这样的汇总吗?
根据窗体上的4个变量条件,分类汇总金额,4个变量条件可以单独使用也可以组合使用,如附件是根据2009年-项目b-施工单位-dd三个变量条件汇总的结果,具体见附件,烦劳各位老师,
...
短信收到
数据库设计不太合理,年份和日期有重复,如:年份选择2009年,日期选择2010-1就不好处理:
Dim cnn As ADODB.Connection
Dim rs As ADODB.Recordset
Private Sub CommandButton1_Click()
Dim s$, arr, i%, SQL$
If ComboBox1.Value <> "" Then
s = s & " and " & Label1.Caption & "='" & ComboBox1.Value & "'"
If ComboBox2.Value <> "" Then s = s & " and month(" & Label2.Caption & ")=val(" & Split(ComboBox2.Value, "-")(1) & ")"
Else
If ComboBox2.Value <> "" Then s = s & " and " & Label1.Caption & "='" & Split(ComboBox2.Value, "-")(0) & "年' and month(" & Label2.Caption & ")=val(" & Split(ComboBox2.Value, "-")(1) & ")"
End If
For i = 3 To 4
If Me.Controls("ComboBox" & i).Value <> "" Then s = s & " and " & Me.Controls("Label" & i).Caption & "='" & Me.Controls("ComboBox" & i).Value & "'"
Next
If s = "" Then Exit Sub
arr = [a3:h3&""]
SQL = "select " & Join(arr, ",") & " from 费用汇总单 where " & Mid(s, 6)
MsgBox SQL
Set rs = New ADODB.Recordset
rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic
With Sheets("查询")
.[A4:H65536].ClearContents
.Range("A4").CopyFromRecordset rs
End With
End Sub |
|