Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Columns().Delete Slowest part of Macro

Status
Not open for further replies.

tqeonline

MIS
Oct 5, 2009
304
US
I have a pretty intense macro and I noticed that it takes baout 30 seconds to run... So I did my redneck timing and put:
Code:
t1 = now()
t2 = now()
...

Then I put a break before the code ends to be able to see the constraints. I found that my macro runs in under 3 seconds if I delete this line of code:
Code:
Columns(a).Delete

Full context:
Code:
For a = ColumnCNT To 21 Step -1
        If Left(UCase(Cells(10, a).Value), 5) <> "TOTAL" Then
            'Columns(a).Delete
            totColumns = totColumns - 1
        ElseIf Cells(10, a).Interior.Color = RGB(238, 236, 225) Then
            totMonths = totMonths + 1
        End If
    Next a
Where ColumnCNT is set to 500. totColumns is set to ColumnCNT at the start. totMonths starts of as Zero (0).

If I allow that line of code it takes an average of 25 seconds to complete.

Question: Is there something I can do to speed up the deletion of columns?

- Matt

"If I must boast, I will boast of the things that show my weakness"

- Windows 2003 Server, 98 SE, XP
- VB.NET, VSTS 2010, ASP.NET, EXCEL VBA, ACCESS, SQL 2008
 
I was able to cut 6-8 seconds off by deleting all the conditional formatting (now down to about 17 for that section):

Code:
.FormatConditions.Delete


- Matt

"If I must boast, I will boast of the things that show my weakness"

- Windows 2003 Server, 98 SE, XP
- VB.NET, VSTS 2010, ASP.NET, EXCEL VBA, ACCESS, SQL 2008
 


application.screenupdating = false
application.calculation = xlCalculationManual

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Got it down to 11 by deleting the other sheets within the workbook.

Code:
    If Sheets.Count > 1 Then
        For Z = Sheets.Count To 2 Step -1
            Sheets(Z).Delete
        Next Z
    End If


- Matt

"If I must boast, I will boast of the things that show my weakness"

- Windows 2003 Server, 98 SE, XP
- VB.NET, VSTS 2010, ASP.NET, EXCEL VBA, ACCESS, SQL 2008
 
Hey Skip - I call two events for all my macros at the start and at the end:

Code:
Function Macro_Start()
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.DisplayAlerts = False
    Application.DisplayStatusBar = False
    Application.EnableEvents = False
End Function
Function Macro_End()
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    Application.DisplayStatusBar = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    MsgBox "Finished"
End Function

- Matt

"If I must boast, I will boast of the things that show my weakness"

- Windows 2003 Server, 98 SE, XP
- VB.NET, VSTS 2010, ASP.NET, EXCEL VBA, ACCESS, SQL 2008
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top