Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-28-2017, 09:02 AM
AndreAndre AndreAndre is offline Excel VB Acceleration Windows 7 32bit Excel VB Acceleration Office 2007
Novice
Excel VB Acceleration
 
Join Date: May 2017
Posts: 1
AndreAndre is on a distinguished road
Default Excel VB Acceleration

I am trying to accelerate my Excel VB Macro significantly. I have tried the 5 alternatives below. I wonder if I could shorten the execution time further.


Sub AccelerateMacro()
'
' v1 052817 by eb+mb
' Macro to copy as fast as possible sheet from one workbook into another workbooks
' Declarations for variables are not shown to make code example more legible
' Macro is stored in and run from "DestinationWorkBook.xlsm"
StartTime = Timer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Alternative = "First"
If Alternative = "First" Then
Workbooks.Open Filename:="SourceWorkBook.xls"
Cells.Select
Selection.Copy
Windows("DestinationWorkBook.xlsm").Activate


Sheets("DestinationSheet").Select
Range("A1").Select
ActiveSheet.Paste
Windows("SourceWorkBook.xls").Activate
ActiveWorkbook.Close
End If
If Alternative = "Second" Then
Workbooks.Open Filename:="SourceWorkBook.xls", ReadOnly:=True
Cells.Select
Selection.Copy
Windows("DestinationWorkBook.xlsm").Activate
Sheets("DestinationSheet").Select
Range("A1").Select
ActiveSheet.Paste
Workbooks("SourceWorkBook.xls").Close SaveChanges:=False
End If
If Alternative = "Third" Then
' I could not get this alternative to work
Workbooks.Open("SourceWorkBook.xls").Worksheets("S ourceSheet").Copy
Workbooks.Open("DestinationWorkBook.xlsm").Workshe ets("DestinationSheet").Range("A1").PasteSpecial
End If
If Alternative = "Fourth" Then
' I could not get this alternative to work
Workbooks.Open("DestinationWorkBook.xlsm").Workshe ets("DestinationSheet").Range("A1") = Workbooks.Open("SourceWorkBook.xls").Worksheets("S ourceSheet")
End If
If Alternative = "Fifth" Then
' I don't understand the code in this alternative
Dim wbIn As Workbook
Dim wbOut As Workbook
Dim rSource As Range
Dim rDest As Range
Set wbOut = Application.Workbooks.Open("DestinationWorkBook.xl sm")
Set wbIn = Application.Workbooks.Open("SourceWorkBook.xls")
With wbIn.Sheets("SourceSheet").UsedRange
wbOut.Sheets("DestinationSheet").Range("A1").Resiz e(.Rows.Count, .Columns.Count) = .Value
End With

SecondsElapsed = Round(Timer - StartTime, 2)
MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
End Sub
Reply With Quote
  #2  
Old 05-29-2017, 09:10 AM
BobBridges's Avatar
BobBridges BobBridges is offline Excel VB Acceleration Windows 7 64bit Excel VB Acceleration Office 2010 32bit
Expert
 
Join Date: May 2013
Location: USA
Posts: 700
BobBridges has a spectacular aura aboutBobBridges has a spectacular aura about
Default

I didn't try to read the whole program to see which techniques you used. But here's a quick list of the things I do to speed up an Excel VBA program:

1) Turn off screen updating (Application.ScreenUpdating); I see you're doing that already.

2) It often helps to turn off automatic calculation (Application.Calculation=xlCalculationManual).

Don't forget to restore these at the end of your program, or if you're abending. I keep a class around to handle both of these settings and a few more; it automatically detects and saves the status of each setting when it's created, and includes a method I can call at the end of the program or during Abend to restore them all before quitting.

3) If you're reading or writing large amounts of data, say more than 100 cells, this one makes a huge difference: Whenever possible, read a range into a two-dimensional array (it has to be two dimensions even if you're looking only at a single column or row), process it there, and write the array back to a range. This is more work at the programming end, but it can turn a three-minute program into a half-second program—well worth the trouble if you're going to use it more than once, and after you get used to using arrays you'll have the techniques down pat and won't think it much of an extra burden.

4) I have the feeling I'm missing one, but I can't remember...
Reply With Quote
Reply

Tags
acceleration, excel vba acceleration



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word & Excel 2010 - Best Options To Auto Insert MySQL & Excel Data? Hoser Word 1 03-17-2017 03:47 PM
Embeding Excel Docs in Word - Receiving Memory Error Message if Excel is open kdash Word 0 05-06-2015 09:38 AM
Excel VB Acceleration How To Open an Macro From 2003 Excel in 2013 Excel Spread Sheet? ADubin Excel Programming 3 02-08-2015 04:57 AM
Excel VB Acceleration [Excel 2007] Building Power Point Slides from data in an Excel Table bremen22 Excel Programming 1 08-07-2013 11:01 AM
Excel VB Acceleration Excel 2011 can't open old Excel 98 or Excel X files FLJohnson Excel 8 05-09-2012 11:26 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:56 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft