Sub xi() Dim h As Long Dim i As Long Dim j As Long Dim x As Long Dim y As Long Dim z As Long t = Timer r = Sheet2.[A65536].End(xlUp).Row Sheet2.Range("a2").Resize(r, 3).sort Key1:=Sheet2.Range("B2"), Key2:=Sheet2.Range("C2"), Key3:=Sheet2.Range("A2") [p2] = Timer - t arr = Sheet2.Cells(1, 1).Resize(r + 2, 4) [p3] = Timer - t For i = 2 To r '为加速做准备 arr(i, 4) = arr(i, 2) & arr(i, 3) Next i ReDim arr1(1 To 2000, 1 To 11) ReDim arr2(1 To r, 1 To 2) As Long [p4] = Timer - t For i = 2 To r If arr(i, 1) & arr(i, 4) = arr(i - 1, 1) & arr(i - 1, 4) Then '地区计数 arr2(x, 2) = arr2(x, 2) + 1 Else x = x + 1 arr2(x, 1) = i arr2(x, 2) = 1 End If jh = jh + 1 If arr(i, 4) = arr(i - 1, 4) Then '故障计数 arr1(y, 4) = arr1(y, 4) + 1 Else y = y + 1 arr1(y, 2) = arr(i, 2) arr1(y, 3) = arr(i, 3) arr1(y, 4) = 1 End If If arr(i, 2) <> arr(i + 1, 2) Then '生成合计 y = y + 1 arr1(y, 2) = arr(i, 2) arr1(y, 3) = "合计" arr1(y, 4) = jh jh = 0 End If Next i For h = 7 To 11 Step 2 j = 1 For i = 1 To y If arr1(i, 3) = "合计" Then i = i + 1 For j = j To x If arr(arr2(j, 1), 4) = arr1(i, 2) & arr1(i, 3) And j < x Then If arr2(j, 2) > arr1(i, h) Then '记录大小和位置 arr1(i, h) = arr2(j, 2) arr1(i, h - 1) = j End If Else If arr1(i, h) > 0 Then arr2(arr1(i, h - 1), 2) = 0 arr1(i, h - 1) = arr(arr2(arr1(i, h - 1), 1), 1) End If Exit For End If Next j Next i Next h arr1(1, 1) = 1 For i = 2 To y If arr1(i, 2) = arr1(i - 1, 2) Then arr1(i, 1) = arr1(i - 1, 1) + 1 Else arr1(i, 1) = 1 End If Next i For i = y To 1 Step -1 If arr1(i, 3) = "合计" Then x = arr1(i, 4) arr1(i, 5) = arr1(i, 4) / x Next i [p5] = Timer - t Cells(3, 1).Resize(y, 11) = arr1 [p6] = Timer - t End Sub
arr = Sheet2.Cells(1, 1).Resize(r + 2, 4) 花0.2812秒 0.344-0.625 Cells(3, 1).Resize(y, 11) = arr1 花0.84秒. 1.594-0.75 数组处理部分 花0.4秒 0.74-0.334 优化空间也只能是这0.4秒,难啊.
[此贴子已经被作者于2007-12-1 14:40:10编辑过] |