Hello,
I have a VBA report I wrote. The report starts by Opening 2x CSV files Goal.csv (18MB) and Objective.csv (40MB) and copying their sheet contents to corresponding sheets in the report. The fastest now I can get the report to open is about 1 min. The open,copy, and paste process is taking up the longest length of the code - approximately 45 sec all together.
Is there a faster way to import this data into the report?
Code Below
I have a VBA report I wrote. The report starts by Opening 2x CSV files Goal.csv (18MB) and Objective.csv (40MB) and copying their sheet contents to corresponding sheets in the report. The fastest now I can get the report to open is about 1 min. The open,copy, and paste process is taking up the longest length of the code - approximately 45 sec all together.
Is there a faster way to import this data into the report?
Code Below
Code:
Private Sub Workbook_Open()
'~~~ 1)
'~~~ The Following closes any worksheets that may have been left open.
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual ' 10-8-2019 Added to Speed up opening of code - This is the turn off
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
Select Case ws.Name
Case "Objective5m", "Goal5m"
Case Else
ws.Delete
End Select
Next ws
Application.DisplayAlerts = True
'~~~ 2)
'~~~ This code resets the autofilter
Sheets("Goal5m").Select
If Range("A2").Value <> "" Then
With ActiveSheet
.AutoFilterMode = False
End With
End If
Sheets("Objective5m").Select
If Range("A2").Value <> "" Then
With ActiveSheet
.AutoFilterMode = False
End With
End If
'~~~ 3)A
' ~~~ This is where the data for GOALS is pulled into the current workbook, which is used to generate the reports.
Dim wbgoal As Workbook
Set wbgoal = Workbooks.Open(Filename:=ThisWorkbook.Path & "\Data\Goal5m.csv")
Application.Windows("Goal5m.csv").Activate 'wbgoal.Activate
wbgoal.ActiveSheet.UsedRange.Copy
ThisWorkbook.Activate
Sheets("Goal5m").Select
Cells.Select
ActiveCell.PasteSpecial xlPasteAll
Application.CutCopyMode = False
Workbooks("Goal5m.csv").Close SaveChanges:=False
'Sheets("Objective5m").Select
Application.DisplayAlerts = True
' Added in 9/2019 - This code will delete all columns that contain a specific text in their header.
' In this case it is deleting any Columns that contain the header of *del
ThisWorkbook.Activate
Sheets("Goal5m").Select
Dim A As Range
Do
Set A = Rows(1).Find(What:="*del*", LookIn:=xlValues, LookAt:=xlPart)
If A Is Nothing Then Exit Do
A.EntireColumn.Delete
Loop
'~~~ 3)B
' ~~~ This is where the data for OBJECTIVES is pulled into the current workbook, which is used to generate the reports.
Dim wbobj As Workbook
Set wbobj = Workbooks.Open(Filename:=ThisWorkbook.Path & "\Data\Objective5m.csv")
Application.Windows("Objective5m.csv").Activate 'wbobj.Activate
wbobj.ActiveSheet.UsedRange.Copy
ThisWorkbook.Activate
Sheets("Objective5m").Select
Cells.Select
ActiveCell.PasteSpecial xlPasteAll
Application.CutCopyMode = False
Workbooks("Objective5m.csv").Close SaveChanges:=False
Sheets("Objective5m").Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.Calculation = xlCalculationAutomatic ' 10-8-2019 Added to Speed up opening of code - This is the turn off