• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Why is VBA running slowly?|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Why is VBA running slowly?|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosWhy is VBA running slowly?
sp_PrintTopic sp_TopicIcon
Why is VBA running slowly?
Avatar
Vivian Wang
Member
Members
Level 0
Forum Posts: 30
Member Since:
March 4, 2020
sp_UserOfflineSmall Offline
1
August 24, 2020 - 10:57 pm
sp_Permalink sp_Print

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

Avatar
Purfleet
England
Member
Members


Trusted Members
Level 4
Forum Posts: 414
Member Since:
December 20, 2019
sp_UserOfflineSmall Offline
2
August 25, 2020 - 2:24 am
sp_Permalink sp_Print

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

Avatar
Philip Treacy
Admin
Level 10
Forum Posts: 1529
Member Since:
October 5, 2010
sp_UserOfflineSmall Offline
3
August 25, 2020 - 8:52 am
sp_Permalink sp_Print

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

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online:
Guest(s) 11
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 873
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 222
Jessica Stewart: 215
A.Maurizio: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Anthony van Riessen
Erlinda Eloriaga
Abisola Ogundele
MARTYN STERRY
Rahim Lakhani
Ngoc Qui Nguyen
Clement Mansfield
Rose .
Bindu Menon
Baruch Zemer
Forum Stats:
Groups: 3
Forums: 24
Topics: 6352
Posts: 27779

 

Member Stats:
Guest Posters: 49
Members: 32319
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.