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
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