由于不信邪,想看看用SQL语句的效果,做了3个小时,终于完工!完全可以达到彭兄要求的效果,但是没有优化,速度就太差了!如果加上数组应该会好一些。但也算是一种解法,凉凉给大家看看!献丑了!!! Dim conn As New ADODB.Connection Dim rst As New ADODB.Recordset Dim rst1 As New ADODB.Recordset Dim rst2 As New ADODB.Recordset Public Sub XTDR() Application.ScreenUpdating = False '关闭屏幕更新 Application.EnableEvents = False '关闭事件响应 'Application.Interactive = False '禁止所有的键盘输入和鼠标输入' Q = Timer Set conn = New ADODB.Connection '创建一个连接和打开 Cnn 连接 Set rst = New ADODB.Recordset '创建一个记录集 conn.Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source=" & ThisWorkbook.FullName Strsql = "Select DISTINCT 机型,故障 from [数据$] ORDER BY 机型 " ''where 机型" & Cells(rowbegin, colbegin) & "='" & Cells(i, 1) & "'" rst.Open Strsql, conn, adOpenKeyset, adLockOptimistic H = rst.RecordCount nn = H 'Sheets("报表").Cells(3, 2).CopyFromRecordset rst T = 0 If nn > 0 Then For I = 1 To nn 'If T = 1 Then GoTo 100 JX = IIf(IsNull(rst.Fields(0)), "", rst.Fields(0)) GZ = IIf(IsNull(rst.Fields(1)), "", rst.Fields(1)) JX1 = Sheets("报表").Cells(T + 2, 2) If JX = JX1 Or T = 0 Then T = T + 1 P = P + 1 Else Sheets("报表").Cells(T + 3, 2) = JX1 Sheets("报表").Cells(T + 3, 3) = "合计" Sheets("报表").Cells(T + 3, 4) = ZSL Sheets("报表").Cells(T + 3, 5) = "100%" Sheets("报表").Cells(T + 3, 1) = P + 1 T = T + 2 P = 1 End If '100: Sheets("报表").Cells(T + 2, 1) = P Sheets("报表").Cells(T + 2, 2) = JX Sheets("报表").Cells(T + 2, 3) = GZ Strsql = "Select DISTINCT K.机型,K.故障,A.总数 AS 总数,B.数量 AS 数量 from (([数据$] K Left JOIN (Select DISTINCT 机型,COUNT(*) AS 总数 from [数据$] where 机型='" & JX & "' GROUP BY 机型) A ON K.机型=A.机型) " & _ " Left JOIN (Select DISTINCT 机型,故障,COUNT(*) AS 数量 from [数据$] where 机型='" & JX & "' AND 故障='" & GZ & "' GROUP BY 机型,故障) B ON K.机型=B.机型 ) where K.机型='" & JX & "' AND K.故障='" & GZ & "'" rst1.Open Strsql, conn, adOpenKeyset, adLockOptimistic H = rst1.RecordCount SL = IIf(IsNull(rst1.Fields(3)), "", rst1.Fields(3)) ZSL = IIf(IsNull(rst1.Fields(2)), "", rst1.Fields(2)) Sheets("报表").Cells(T + 2, 4) = SL Sheets("报表").Cells(T + 2, 5) = Format(SL / ZSL, "#,00%") rst1.Close Strsql = "Select DISTINCT 省份,COUNT(*) AS 数量 from [数据$] where 机型='" & JX & "' AND 故障='" & GZ & "' GROUP BY 省份 ORDER BY COUNT(*)DESC " rst2.Open Strsql, conn, adOpenKeyset, adLockOptimistic H = rst2.RecordCount D = 0 If H >= 3 Then L = 3 Else L = H End If For B = 1 To L SF = IIf(IsNull(rst2.Fields(0)), "", rst2.Fields(0)) SFSL = IIf(IsNull(rst2.Fields(1)), "", rst2.Fields(1)) Sheets("报表").Cells(T + 2, 6 + (D * 2)) = SF Sheets("报表").Cells(T + 2, 7 + (D * 2)) = SFSL D = D + 1 rst2.MoveNext Next B rst2.Close rst.MoveNext Next I Sheets("报表").Cells(T + 3, 2) = JX1 Sheets("报表").Cells(T + 3, 3) = "合计" Sheets("报表").Cells(T + 3, 4) = ZSL Sheets("报表").Cells(T + 3, 5) = "100%" Sheets("报表").Cells(T + 3, 1) = P + 1 End If rst.Close Set conn = Nothing Set rst = Nothing Set rst1 = Nothing Set rst2 = Nothing Sheets("报表").Cells(T + 4, 1) = Timer - Q Application.Interactive = True Application.EnableEvents = True Application.ScreenUpdating = True End Sub
o8jYXrYI.rar
(21.4 KB, 下载次数: 10)
|