Modify and streamline code for speed and efficiency

linesite

New Member
Joined
Oct 10, 2009
Messages
41
This code works but it takes a few minutes to complete depending on the number of worksheets in the original workbook (see link below). The time is not of great concern; however, my laptop at times slow down considerably or freezes altogether during execution of the code. Is there a way to streamline and make the code more efficient? Is there code that will skip the raw data and pivot table worksheet?

Sub Autofit_Columns()

Dim mySheet As Worksheet
Dim myRange As Range
Dim LastRow As Long

Application.ScreenUpdating = False

For Each mySheet In ActiveWorkbook.Worksheets
With mySheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set myRange = Range(.Cells(1, "A"), .Cells(LastRow, "H"))
End With
With myRange
.EntireColumn.AutoFit
.HorizontalAlignment = xlLeft
End With

mySheet.Copy

ActiveWorkbook.SaveAs Filename:="C:\Users\dbell\Downloads\Zonar\Last Phone Home & Position Date\" & _
"GPS units report - " & ActiveSheet.Name & " " & "-" & " " & Format(Date, "mmm. dd-yyyy") & _
".xlsm", FileFormat:=52

Next mySheet

Application.ScreenUpdating = True

Set mySheet = Nothing
Set myRange = Nothing

End Sub


Link to Workbook:

https://1drv.ms/x/s!AkQDFpxhg2E8l2_rbvH8cU5xpgOD
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Did you intend to keep all those workbooks open? Except for adding the Enableevents and Calculation statements, I couldn't see a way to improve the time.


Code:
Sub Autofit_Columns()


  Dim mySheet As Worksheet
  Dim myRange As Range
  Dim LastRow As Long
  
  
  Application.ScreenUpdating = False
 [COLOR=#0000ff] Application.EnableEvents = False
  Application.Calculation = xlCalculationManual[/COLOR]
  
  For Each mySheet In ActiveWorkbook.Worksheets
    With mySheet
      LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
      Set myRange = Range(.Cells(1, "A"), .Cells(LastRow, "H"))
    End With
    With myRange
      .EntireColumn.AutoFit
      .HorizontalAlignment = xlLeft
    End With
  
    mySheet.Copy
    
    ActiveWorkbook.SaveAs Filename:="[COLOR=#574123]C:\Users\dbell\Downloads\Zonar\Last Phone Home & Position Date[/COLOR]\" & _
    "GPS units report - " & ActiveSheet.Name & " " & "-" & " " & Format(Date, "mmm. dd-yyyy") & _
    ".xlsm", FileFormat:=52
[COLOR=#ff0000]    ActiveWorkbook.Close savechanges:=False[/COLOR]
    
  
  Next mySheet
  
  Application.EnableEvents = True
  Application.Calculation = xlCalculationAutomatic
  Application.ScreenUpdating = True
  
  Set mySheet = Nothing
  Set myRange = Nothing


End Sub
 
Upvote 0
I could close each workbook after saving--ultimately I will create an email template for each cost center and email workbook to operation and yard managers so it's not necessary that each workbook remain open.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,608
Messages
6,173,325
Members
452,510
Latest member
RCan29

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