|
发表于 2013-3-3 13:09
|
显示全部楼层
本楼为最佳答案
本帖最后由 hwc2ycy 于 2013-3-3 13:18 编辑
继续改,又发现问题了,,上面一排的条件均可引用。- Private Sub CommandButton1_Click()
- Range("M5:U65535").ClearContents
- Dim cn As New ADODB.Connection, sql As String
- Dim SQdate As String
- Application.ScreenUpdating = False
- t = Timer
- Dim SQ1$, SQ2$
- Dim MROW&, XROW&
- MROW = Sheets("sheet1").Range("B65536").End(xlUp).Row '取工作表有数据的最大行号,本例以E列
- XROW = Sheets("sheet2").Range("B65536").End(xlUp).Row
- If Len([N1]) > 0 And Len([R1]) > 0 Then SQdate = " WHERE 日期 between #" & [N1] & "# AND #" & [R1] & "# "
- SQ2 = " where "
- If Len([m3]) > 0 Then SQ2 = SQ2 & [m2] & " like " & "'" & [m3] & "'" & " and "
- If Len([n3]) > 0 Then SQ2 = SQ2 & [n2] & " like " & "'" & [n3] & "'" & " and "
- If Len([o3]) > 0 Then SQ2 = SQ2 & [o2] & " like " & "'" & [o3] & "'" & " and "
- If Len([p3]) > 0 Then SQ2 = SQ2 & [p2] & " like " & "'" & [p3] & "'" & " and "
- If Len([q3]) > 0 Then SQ2 = SQ2 & [q2] & " like " & "'" & [q3] & "'" & " and "
- If Len([r3]) > 0 Then SQ2 = SQ2 & " 量 " & " >= " & [r3] & " and "
- If Len([s3]) > 0 Then SQ2 = SQ2 & [s2] & " like " & "'" & [s3] & "'" & " and "
- If Len([t3]) > 0 Then SQ2 = SQ2 & " 金额 " & ">=" & [t3] & " and "
- SQ2 = Left(SQ2, Len(SQ2) - 5)
- cn.Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source=" & ThisWorkbook.FullName
- sql = "select * from (select 货品编号,供应商,产地,品名,规格,sum(进货数量) as 量,单位,sum(进货总金额) as 金额 from [sheet1$B4:K]" & SQdate & " GROUP BY 货品编号,供应商,产地,品名,规格,单位) "
- If Len(SQ2) > 8 Then
- sql = sql & SQ2
- End If
- Sheet1.Range("m5").CopyFromRecordset cn.Execute(sql)
- cn.Close
- Set cn = Nothing
- Application.ScreenUpdating = True
- MsgBox "查询完成"
- End Sub
复制代码 |
|