Thoughts to speed up this macro?

billfinn

Board Regular
Joined
Jun 7, 2018
Messages
114
Office Version
  1. 2016
Platform
  1. Windows
Good morning!
I have a macro that functions as it should but is slow. The macro strips out all of the report and page headers from the text file dumped from the AS400 and organizes the sheet in the desired manner. I realize that part of the reason it's slow is the size of the worksheet it's formatting (62,743 rows) but I'm hoping some of you folks might have ideas that would help me to speed this process up? All suggestions and comments are very much appreciated.
Thanks much!
Bill
Code:
Sub Strip_Page_Report_Headers()

    'Macro to strip out Report and Page headings to leave an Excel File with columns properly formatted.
    On Error GoTo ErrorHandler
Application.EnableEvents = False
    
    Windows("Estimator.xlsm").Activate  'Go to the Estimating Tool
    Sheets("Item Master").Select      'Select the Test Data worksheet in the Estimating tool
    Selection.EntireColumn.Hidden = False
    Dim Ary As Variant
    Dim i As Long
    Ary = Array("*QUERY*", "*INVMASTB*", "*INVMASTAAA*", "*LIBRARY*", "*PRICEMSM*", "*DATE*", "TIME*", "*info*", "*PAGE*", "Pricing", "*Cost*", "*Page*", "*DELETED*", "*EDIORGI*", "*DELETE*", "*FORMAT*")
    For i = 0 To UBound(Ary)
    Range("A:O").Replace Ary(i), "=xxx", xlWhole, , False, , False, False
    Next i


      
For i = 1 To 15
   On Error Resume Next
   Columns(i).SpecialCells(xlFormulas, xlErrors).EntireRow.Delete
   On Error GoTo 0
Next






'Add Column Labels
Rows(1).Insert Shift:=xlDown
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "ITEM"
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "DESCRIPTION"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "COST"
   
    ActiveCell.FormulaR1C1 = "Price"
  ' Set Column C as currency
Worksheets("Item Master").Columns("C").NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"


Columns.AutoFit
  
ErrorHandler:
    Application.EnableEvents = True


End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
At the start of your code, replace Application.EnableEvents = False with this:
Code:
With Application
    .EnableEvents = False
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
and at the end replace Application.EnableEvents = True with this:
Code:
With Application
    .EnableEvents = True
    .ScreenUpdating = True
    .Calculation = xlCalculationAuotomatic
End With
 
Upvote 0
Also, removing "Select" statements will speed up your code. Almost any time you have a row that ends with "Select" followed by a row that begins with "Selection" or "ActiveCell", you can combine those two rows together.

So all of this:
Code:
    Range("A1").Select
    ActiveCell.FormulaR1C1 = "ITEM"
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "DESCRIPTION"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "COST"
can be reduced to this:
Code:
    Range("A1").FormulaR1C1 = "ITEM"
    Range("B1").FormulaR1C1 = "DESCRIPTION"
    Range("C1").FormulaR1C1 = "COST"
 
Upvote 0
JoeMo and Joe4,
Thanks so much for the input. I am very grateful. Waiting on a new text file and a test. I will report results later
Thanks,
Bill
 
Upvote 0
You are welcome.
Glad "the Joes" were able to help!:)
 
Upvote 0
JoeMo and Joe4,
Just wanted to get back to you both with an update. The old Elapsed time for this macro was around 2 minutes 54 seconds. New elapsed time is 1 minute 56 seconds. For a file this large that is a significant improvement!
Thanks again for your suggestions.
Bill
 
Upvote 0
JoeMo and Joe4,
Just wanted to get back to you both with an update. The old Elapsed time for this macro was around 2 minutes 54 seconds. New elapsed time is 1 minute 56 seconds. For a file this large that is a significant improvement!
Thanks again for your suggestions.
Bill
As Joe said (Joe Who?) - Glad "the Joes" were able to help!:)
 
Upvote 0

Forum statistics

Threads
1,215,035
Messages
6,122,785
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top