March 4, 2020
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 "\\peofpaup012\Accounting\Reporting - Weekly\P&L\2020-21\NSW"
Workbooks.Open Filename:= _
"\\peofpaup012\Accounting\Reporting - Weekly\P&L\2020-21\NSW\pl 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:= _
"\\peofpaup012\Accounting\Reporting - Weekly\P&L\2020-21\QLD\pl 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:= _
"\\peofpaup012\Accounting\Reporting - Weekly\P&L\2020-21\WASANT\pl 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:= _
"\\peofpaup012\Accounting\Reporting - Weekly\P&L\2020-21\VIC\pl 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:= _
"\\peofpaup012\Accounting\Reporting - Weekly\P&L\2020-21\HO\pl 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:= _
"\\peofpaup012\Accounting\Reporting - Weekly\P&L\2020-21\Consolidated\pl 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:="\\peofpaup012\Accounting\Reporting - Weekly\P&L\2020-21\P&L Summary 20-21 Wk " & NextWeekNo & ".xls"
End Sub
Trusted Members
December 20, 2019
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
October 5, 2010
Hi Vivian,
Please refer to this
https://www.myonlinetraininghu.....this-first
A topic title of Help doesn't tell us much! and as Purfleet said, you should always attach your workbook.
Regards
Phil
1 Guest(s)