How can I make this code execute quicker?

storemannequin

Board Regular
Joined
May 29, 2010
Messages
108
Code:
Sub WeeklyNotificationV2()

Dim ActveWb As Workbook, SlsWb As Workbook
Dim ActveWs As Worksheet, SlsWs As Worksheet
Dim FnlRw As Long, FnlCol As Long, i As Long

Application.ScreenUpdating = False

Set ActveWb = ActiveWorkbook
Set ActveWs = ActveWb.ActiveSheet

FnlRw = ActveWs.Cells(Rows.Count, 1).End(xlUp).Row + 1
FnlCol = ActveWs.Cells(1, Columns.Count).End(xlToLeft).Column

With Range("B2:B" & FnlRw)
    .Replace "/", " ", xlPart
End With

LastPln = ActveWs.Cells(2, 1).Value
LastNme = ActveWs.Cells(2, 2).Value
StartRw = 2


For i = 2 To FnlRw
  ThisPln = ActveWs.Cells(i, 1).Value
  ThisNme = ActveWs.Cells(i, 2).Value
  
    If ThisPln = LastPln Then
    
     Else
        LastRw = i - 1
        Rowcount = LastRw - StartRw + 1
        
    Set SlsWb = Workbooks.Add(template:=xlWBATWorksheet)
    Set SlsWs = SlsWb.Worksheets(1)
    
    ActveWs.Range(ActveWs.Cells(1, 3), ActveWs.Cells(1, FnlCol)).Copy Destination:=SlsWs.Range("A1")
    
    ActveWs.Range(ActveWs.Cells(StartRw, 3), ActveWs.Cells(LastRw, FnlCol)).Copy _
    Destination:=SlsWs.Cells(2, 1)
    
    SlsWs.Name = "UOM Same"
    

'-------------------------------------------------
    With SlsWs.PageSetup
        .PrintTitleRows = "$1:$1"
        .PrintTitleColumns = ""
        .LeftHeader = "&""Arial,Bold""&12Sales" & Chr(10) & LastPln & Chr(10)
        .CenterHeader = _
        "&""Calibri,Bold""&14" & LastNme & Chr(10) & " Discontinued List" & Chr(10)
        .RightHeader = "&""Arial,Bold""&12Date Created:" & Chr(10) & Date
        .LeftFooter = "&F DK"
        .CenterFooter = "&8&P of &N" & Chr(10) & "Information"
        .CenterFooter = "Page &P of &N" & Chr(10) & "Information"
        .CenterHorizontally = True
        .Orientation = xlLandscape
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1000
        .PrintGridlines = True
        
    End With
'-------------------------------------------------


         With SlsWs.Rows("2:2")
             .VerticalAlignment = xlBottom
             .WrapText = False
             
        End With
   
   Cells.EntireColumn.AutoFit
   
   
   
    FN = LastNme & " Discontinued Item List " & Format(Date, "mm-dd-yyyy") & ".xlsx"
    FP = ActveWb.Path & Application.PathSeparator
    
    SlsWb.SaveAs Filename:=FP & FN
    SlsWb.Close savechanges:=False
    
    
    LastPln = ThisPln
    LastNme = ThisNme
    StartRw = i
  
  End If
    
Next i

End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
All I can see is that you have the line of code "Cells.EntireColumn.AutoFit" which will work with all cells in the worksheet. It is usually better to just work with the "used" cells.
You could use something like, "ActiveSheet.UsedRange.EntireColumn.AutoFit".
Also, having it inside a loop will cause it to run each time the loop cycles.

Beware of "Volatile Formulas", they slow down each calculation. Do a search on this forum for suggestions on Volatile Formulas.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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