|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 lsdongjh 于 2018-8-22 13:21 编辑
- Sub Test()
- Dim strShName As String, shSource As Worksheet, shResult As Worksheet
- Dim lngRows As Long, strTemp As String, strDate As String
- Dim Conn As Object, Rst As Object, strPath As String
- Dim strConn As String, strBasicSql As String, strCondition As String, strSQL As String
- Dim arrResult(1 To 9, 1 To 1) As Double
- strShName = "1"
- Set shSource = Sheets(strShName)
- Set shResult = Sheets("2")
-
- lngRows = shSource.Range("A" & Rows.Count).End(xlUp).Row
- If lngRows < 4 Then lngRows = 4
-
- strTemp = shResult.Range("O1").Value
- If Trim(strTemp) = "" Or Not IsDate(strTemp) Then
- MsgBox "请输入【开始日期】"
- shResult.Range("O1").Select
- Exit Sub
- End If
-
- strTemp = shResult.Range("O2").Value
- If Trim(strTemp) = "" Or Not IsDate(strTemp) Then
- MsgBox "请输入【结束日期】"
- shResult.Range("O2").Select
- Exit Sub
- End If
- strDate = strTemp
-
- strCondition = "AND 日期 Between #" & shResult.Range("O1").Value & "# AND #" & shResult.Range("O2").Value & "# "
- strTemp = shResult.Range("O3").Value
- If Trim(strTemp) <> "" Then strCondition = strCondition & " AND 编号='" & Trim(strTemp) & "'"
- strTemp = shResult.Range("O4").Value
- If Trim(strTemp) <> "" Then strCondition = strCondition & " AND 名称='" & Trim(strTemp) & "'"
- strTemp = shResult.Range("O5").Value
- If Trim(strTemp) <> "" Then strCondition = strCondition & " AND 时间段='" & Trim(strTemp) & "'"
-
- Set Conn = CreateObject("ADODB.Connection")
- Set Rst = CreateObject("ADODB.Recordset")
- strPath = ThisWorkbook.FullName
- Select Case Application.Version * 1
- Case Is <= 11
- strConn = "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=excel 8.0;Data source=" & strPath
- Case Is >= 12
- strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strPath & ";Extended Properties=""Excel 12.0;HDR=YES"";"""
- End Select
- Conn.Open strConn
-
- strBasicSql = "SELECT Sum(使用数量) AS 数量合计, Sum(合计金额) AS 金额合计, Min(合计金额) AS 最小金额, Max(合计金额) AS 最大金额 " & _
- "FROM [" & strShName & "$A3:M" & lngRows & "]" & _
- "WHERE 1=1 "
- strSQL = strBasicSql & strCondition
-
- If Rst.State = 1 Then Rst.Close
- Rst.Open strSQL, Conn, 3, 1
- Rst.movefirst
- arrResult(6, 1) = IIf(IsNull(Rst("最小金额")), 0, Rst("最小金额"))
- arrResult(7, 1) = IIf(IsNull(Rst("最大金额")), 0, Rst("最大金额"))
- arrResult(8, 1) = IIf(IsNull(Rst("数量合计")), 0, Rst("数量合计"))
- arrResult(9, 1) = IIf(IsNull(Rst("金额合计")), 0, Rst("金额合计"))
-
- strBasicSql = "SELECT SUM(金额) AS 合计金额 " & _
- "FROM (SELECT 日期, Sum(合计金额) AS 金额 " & _
- "FROM [" & strShName & "$A3:M" & lngRows & "]" & _
- "WHERE (1=1 @Condition) " & _
- "GROUP BY 日期 ) " & _
- "WHERE 日期>=#@Date#"
- strBasicSql = Replace(strBasicSql, "@Condition", strCondition)
-
- strTemp = DateAdd("d", -2, strDate)
- strSQL = Replace(strBasicSql, "@Date", strTemp)
- If Rst.State = 1 Then Rst.Close
- Rst.Open strSQL, Conn, 3, 1
- arrResult(1, 1) = IIf(IsNull(Rst("合计金额")), 0, Rst("合计金额") / 3)
-
- strTemp = DateAdd("d", -4, strDate)
- strSQL = Replace(strBasicSql, "@Date", strTemp)
- If Rst.State = 1 Then Rst.Close
- Rst.Open strSQL, Conn, 3, 1
- arrResult(2, 1) = IIf(IsNull(Rst("合计金额")), 0, Rst("合计金额") / 5)
-
- strTemp = DateAdd("d", -9, strDate)
- strSQL = Replace(strBasicSql, "@Date", strTemp)
- If Rst.State = 1 Then Rst.Close
- Rst.Open strSQL, Conn, 3, 1
- arrResult(3, 1) = IIf(IsNull(Rst("合计金额")), 0, Rst("合计金额") / 10)
-
- strTemp = DateAdd("d", -14, strDate)
- strSQL = Replace(strBasicSql, "@Date", strTemp)
- If Rst.State = 1 Then Rst.Close
- Rst.Open strSQL, Conn, 3, 1
- arrResult(4, 1) = IIf(IsNull(Rst("合计金额")), 0, Rst("合计金额") / 15)
-
- strTemp = DateAdd("d", -29, strDate)
- strSQL = Replace(strBasicSql, "@Date", strTemp)
- If Rst.State = 1 Then Rst.Close
- Rst.Open strSQL, Conn, 3, 1
- arrResult(5, 1) = IIf(IsNull(Rst("合计金额")), 0, Rst("合计金额") / 30)
-
- shResult.Range("O7:O15") = arrResult
- Set Rst = Nothing
- Set Conn = Nothing
- End Sub
复制代码 |
|