Forum

Why is VBA running ...
 
Notifications
Clear all

Why is VBA running slowly?

3 Posts
3 Users
0 Reactions
122 Views
(@vivian726)
Posts: 30
Trusted Member
Topic starter
 

Dear Mynda,

I have bought courses of other topic but not VBA, not sure if I should post it here or?

 

Could you please have a check below VBA, it takes around 20 mins or even longer to run.

Is it possible to rewrite it to make it faster? So for the new week, we run macro to get different states PL into PL summary file, they are from different workbooks.

Your kind help would be greatly appreciately.

 

Sub CreateNewWeek()

Dim OrigWeekNo As String
Dim NextWeekNo As String

Application.ScreenUpdating = False

ActiveSheet.Unprotect
Range("A3").Select
OrigWeekNo = ActiveCell.Value
If OrigWeekNo = 0 Then GoTo CopyData

Sheets("P&L Summary").Activate

CopyData:

Range("A3").Select

'Set Rng = Range("A3")
NextWeekNo = OrigWeekNo + 1
ActiveCell.Formula = NextWeekNo

ChDir "\peofpaup012AccountingReporting - WeeklyP&L2020-21NSW"
Workbooks.Open Filename:= _
"\peofpaup012AccountingReporting - WeeklyP&L2020-21NSWpl 2020-21 NSW Wk " & NextWeekNo & ".xls"
'ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Application.Goto Reference:="Copy_To_Summary"
Selection.Copy
Windows("P&L Summary 20-21 Wk " & OrigWeekNo & ".xls").Activate
Sheets("Data").Select
Application.Goto Reference:="NSW_Data"
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("pl 2020-21 NSW Wk " & NextWeekNo & ".xls").Activate
Application.CutCopyMode = False
ActiveWindow.Close SaveChanges:=False
Workbooks.Open Filename:= _
"\peofpaup012AccountingReporting - WeeklyP&L2020-21QLDpl 2020-21 QLD Wk " & NextWeekNo & ".xls"
'ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Application.Goto Reference:="Copy_To_Summary"
Selection.Copy
Windows("P&L Summary 20-21 Wk " & OrigWeekNo & ".xls").Activate
Sheets("Data").Select
Application.Goto Reference:="QLD_Data"
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("pl 2020-21 QLD Wk " & NextWeekNo & ".xls").Activate
Application.CutCopyMode = False
ActiveWindow.Close SaveChanges:=False
Workbooks.Open Filename:= _
"\peofpaup012AccountingReporting - WeeklyP&L2020-21WASANTpl 2020-21 WASANT Wk " & NextWeekNo & ".xls"
'ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Application.Goto Reference:="Copy_To_Summary"
Selection.Copy
Windows("P&L Summary 20-21 Wk " & OrigWeekNo & ".xls").Activate
Sheets("Data").Select
Application.Goto Reference:="WASANT_Data"
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("pl 2020-21 WASANT Wk " & NextWeekNo & ".xls").Activate
Application.CutCopyMode = False
ActiveWindow.Close SaveChanges:=False
Workbooks.Open Filename:= _
"\peofpaup012AccountingReporting - WeeklyP&L2020-21VICpl 2020-21 VIC Wk " & NextWeekNo & ".xls"
'ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Application.Goto Reference:="Copy_To_Summary"
Selection.Copy
Windows("P&L Summary 20-21 Wk " & OrigWeekNo & ".xls").Activate
Sheets("Data").Select
Application.Goto Reference:="VIC_Data"
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows("pl 2020-21 VIC Wk " & NextWeekNo & ".xls").Activate
Application.CutCopyMode = False
ActiveWindow.Close SaveChanges:=False
Workbooks.Open Filename:= _
"\peofpaup012AccountingReporting - WeeklyP&L2020-21HOpl 2020-21 HO Wk " & NextWeekNo & ".xls"
'ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Application.Goto Reference:="Copy_To_Summary"
Selection.Copy
Windows("P&L Summary 20-21 Wk " & OrigWeekNo & ".xls").Activate
Sheets("Data").Select
Application.Goto Reference:="HO_Data"
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A3").Select
Windows("pl 2020-21 HO Wk " & NextWeekNo & ".xls").Activate
Application.CutCopyMode = False
ActiveWindow.Close SaveChanges:=False

Workbooks.Open Filename:= _
"\peofpaup012AccountingReporting - WeeklyP&L2020-21Consolidatedpl 2020-21 Consolidated Wk " & NextWeekNo & ".xls"
'ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Application.Goto Reference:="Copy_To_Summary"
Selection.Copy
Windows("P&L Summary 20-21 Wk " & OrigWeekNo & ".xls").Activate
Sheets("Data").Select
Application.Goto Reference:="Nat_Data"
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A3").Select
Windows("pl 2020-21 Consolidated Wk " & NextWeekNo & ".xls").Activate
Application.CutCopyMode = False
ActiveWindow.Close SaveChanges:=False

Application.Goto Reference:="actual_copy_YTD"
Selection.Copy
Sheets("YTD").Select
Range("D5").Select
ActiveCell.Offset(0, 1).Select
Do While Not IsEmpty(ActiveCell)
ActiveCell.Offset(0, 1).Select
Loop
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False

Windows("P&L Summary 20-21 Wk " & OrigWeekNo & ".xls").Activate

Sheets("P&L Summary").Activate
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True

ActiveWorkbook.SaveAs Filename:="\peofpaup012AccountingReporting - WeeklyP&L2020-21P&L Summary 20-21 Wk " & NextWeekNo & ".xls"

End Sub

 
Posted : 25/08/2020 8:57 am
(@purfleet)
Posts: 412
Reputable Member
 

Just a quick comment - with out seeing the data in the reports/extracts that the macro is opening it would be very difficult to know why the macro takes so long.

On the face of it the Macro isnt doing a great deal other than opening old excel files (.xls) and copying and pasting. There is also a lot of redunant code in there

The first things i would look at is the file sizes and how long they take to open. 

The only other thing i noticed was the 'Do While' which might take a long time if there are a lot of used rows - if there are a lot of rows in use you might be able to get a quick hit from changing the Range("D5").select to a higher number than is still used (for example if you data goes to row 5000, change this to range("D4995").select and test.

Application.Goto Reference:="actual_copy_YTD"
Selection.Copy
Sheets("YTD").Select
Range("D5").Select
ActiveCell.Offset(0, 1).Select
Do While Not IsEmpty(ActiveCell)
ActiveCell.Offset(0, 1).Select
Loop
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False

 
Posted : 25/08/2020 12:24 pm
Philip Treacy
(@philipt)
Posts: 1629
Member Admin
 

Hi Vivian,

Please refer to this

https://www.myonlinetraininghub.com/excel-forum/forum-rules-and-guides/read-this-first

A topic title of Help doesn't tell us much!  and as Purfleet said, you should always attach your workbook.

Regards

Phil

 
Posted : 25/08/2020 6:52 pm
Share: