Excel - Speed up VBA process for loop in formatting - the numbers

mercredi 29 avril 2015

A little rehash of speeding up things using Excel Automation.
Using MSAccess, a Recordset of just over 30,000 records is pulled from SQL Server and placed into Excel using Excel automation.

The last Column 0 contains a number of 1 to 4.
Sort is by "Well Name" then by the 1 to 4.

This means that every time there is a new Well Name - there is a cooresponding number 1 in column O.
The rows with number 4 are a zero to many for the Well.
All together, there is a about 10,000 number 4.

The code has a loop to check each record to the RecordSet.Count.
Inside the loop, the Code consist of an If - Then - each with a nested If-Then.

Running this on a Pentium 3 Gen i7, the code with out the speed up took 7.75 Minutes to complete (just the loop)
Code:

' speedup ////////////////////////////////////////////////////// speedup code///  turn off to troubleshoot or debug
  objxl.ScreenUpdating = False
  objxl.Calculation = xlCalculationManual 'To turn off the automatic calculation
  objxl.EnableEvents = False
  objxl.ActiveSheet.DisplayPageBreaks = False
' speedup ////////////////////////////////////////////////////// speedup code///  turn off to troubleshoot or debug
 
2790    With objxl.ActiveWorkbook.ActiveSheet
          'objxl.ActiveWorkbook.ActiveSheet
2800          For i = intRowPos To intMaxRecordCount + intRowPos
2810            'If .Cells(i, "B").Value <> .Cells(i - 1, "B").Value Then
                If .Cells(i, "O").Value = 1 Then  ' 1st sorted order for Lease type
2820                    .Range(.Cells(i, "B"), .Cells(i, "J")).Font.FontStyle = "Bold"
                        .Range(.Cells(i, "B"), .Cells(i, "J")).Borders(xlTop).ColorIndex = xlAutomatic
                                    ' must set back to automatic xince Else statement changes style
                        .Range(.Cells(i, "B"), .Cells(i, "J")).Borders(xlTop).Weight = xlThick
 
2830            Else
2840                .Range(.Cells(i, "B"), .Cells(i, "F")).Font.ColorIndex = 16 'metalic gray
                    If .Cells(i, "O").Value = 4 Then
                            .Range(.Cells(i, "G"), .Cells(i, "H")).Font.FontStyle = "Bold"
                    End If
2850            End If
2860        Next i
2870  End With
          ' end bold columns on changed value  7.75 min for loop
        ' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Bold and Lighten <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
' speedup ////////////////////////////////////////////////////// speedup code///  turn off to troubleshoot or debug
        objxl.ScreenUpdating = False
        objxl.Calculation = xlCalculationManual 'To turn off the automatic calculation
        objxl.EnableEvents = False
        objxl.ActiveSheet.DisplayPageBreaks = False
' speedup ////////////////////////////////////////////////////// speedup code///

With the Speedup Code as shown above, the same process took
1.25 Minutes.
Excel - Speed up VBA process for loop in formatting - the numbers

0 commentaires:

Enregistrer un commentaire

Labels