以下是引用office2008在2008-9-2 16:42:30的发言:虽然你以后肯定会想提高速度的,但目的是达到了,也就是说成功了,一个字"牛" [em17][em17][em17] 下面代码已经优化了,速度应该没问题了,呵呵 Sub fig() With Worksheets("连续3天失败标记") Set CNN = CreateObject("adodb.connection") CNN.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;hdr=yes;';Data Source=" & ThisWorkbook.FullName .Range("f2:i1000").ClearContents r = .Range("a65536").End(xlUp).Row Sql = "select a.num,a.operation,a.dt,a.errtype FROM [连续3天失败标记$a1:d888] a,[连续3天失败标记$a1:d888] b,[连续3天失败标记$a1:d888] c where a.num=b.num and a.num=c.num and b.num=c.num and a.operation=b.operation and a.operation=c.operation and b.operation=c.operation and day(b.dt)-day(a.dt)=1 and day(c.dt)-day(b.dt)=1 and day(c.dt)-day(a.dt)=2" Sql = Sql & " union (select b.num,b.operation,b.dt,b.errtype FROM [连续3天失败标记$a1:d888] a,[连续3天失败标记$a1:d888] b,[连续3天失败标记$a1:d888] c where a.num=b.num and a.num=c.num and b.num=c.num and a.operation=b.operation and a.operation=c.operation and b.operation=c.operation and day(b.dt)-day(a.dt)=1 and day(c.dt)-day(b.dt)=1 and day(c.dt)-day(a.dt)=2)" Sql = Sql & " union (select c.num,c.operation,c.dt,c.errtype FROM [连续3天失败标记$a1:d888] a,[连续3天失败标记$a1:d888] b,[连续3天失败标记$a1:d888] c where a.num=b.num and a.num=c.num and b.num=c.num and a.operation=b.operation and a.operation=c.operation and b.operation=c.operation and day(b.dt)-day(a.dt)=1 and day(c.dt)-day(b.dt)=1 and day(c.dt)-day(a.dt)=2)" Sql = " select distinct num ,operation , dt , errtype from (" & Sql & ") where len(num)>0" Sql = " select aa.num,aa.operation,aa.dt,aa.errtype from [连续3天失败标记$a1:d888] aa,(" & Sql & ") bb where aa.num=bb.num and aa.operation=bb.operation and aa.dt=bb.dt and aa.errtype=bb.errtype" .Range("f2").CopyFromRecordset CNN.Execute(Sql) End With End Sub
|