vba speed issues

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » VBA Speed Issues: 2003 to 2007

VBA Speed Issues: 2003 to 2007

resolvedResolved · Medium Priority · Version 2007

Ben has attended:
Excel VBA Intro Intermediate course
Excel VBA Advanced course

VBA Speed Issues: 2003 to 2007

Hi,

I've been looking at a Projet Finance Model that we use in house that utilises a VBA macro as part of deciding how much debt we can use in a project that we finance. The model itself is excel heavy and VBA light(ish). The VBA is kept pretty much down to Goal seeks and a few copy/paste operations in order to break a circular relating to interest charged during construction. The VBA does iterate a number of times tho.

My problem is that if I open the file in Excel 2003, running a single case takes the model about 9seconds to run through . If import the same version into 2007, save it as macro enabled 2007, then reopen it (i.e. same version just in 2007) then run the model through it took 26 seconds to finish. (& Yes i feel somewhat ashamed in timing an Excel spreadsheet but forgive me.....)

My key questions is why the difference? Is this an Excel problem or a VBA problem? Can you offer me some commonly known issues with 2007 that make the process slower? Additionally, with the code below is there any way that I could speed it up? Is it necessary to clear out the memory after operations?

The code is as follows

Sub Run_Cases()

For x = Range("upper_case") To Range("lower_case") Step -1 ' steps through the defined cases ending with 1, ie the base case


Range("case") = x

If Range("Fee_Calculation").Value = "On" Then

Range("Development_Fee") = 0
Call Model_Optimisation
Range("Development_Fee") = Range("Fee").Value
End If

Call Model_Optimisation
Range("case" & x).Formula = Range("current").Value

Next x

End Sub

Sub Model_Optimisation()

Application.ScreenUpdating = Range("Screen_update").Value ' refers to excel sheet to see if screen updating is switched off (off is quicker)
Range("proj1_afroz2") = 0 ' resets any variables
Range("proj1_Bfroz2") = 0
Range("Mezzcap1") = 0
Range("Mezz_froz2") = 0

Dim n As Integer
For n = 1 To 5

Range("wind") = Range("bank_wind").Value ' sets assumptions to bank case
Range("tariff") = "Bank"
Range("BANK_DIFF").GoalSeek goal:=0, changingcell:=Range("BANK_EQ")

If Range("Leverage_Switch").Value = "Off" Then
Range("proj1_afroz2") = 0
Range("proj1_Bfroz2") = 0
Range("Mezzcap1") = 0
Range("Mezz_froz2") = 0
End If

If Range("Tranche_A_Switch").Value = "On" Then Proj1_OptA Else Range("proj1_afroz2") = 0
If Range("Tranche_B_Switch").Value = "On" Then Proj1_OptB Else Range("proj1_Bfroz2") = 0
If Range("Mezzanine_Switch").Value = "On" Then
Range("tariff") = Range("Mezz_Tariff").Value
Call Mezzanine_Opt
Else
Range("Mezzcap1") = 0
Range("Mezz_froz2") = 0
End If

Next n

Range("BANK_DIFF").GoalSeek goal:=0, changingcell:=Range("BANK_EQ")
Range("wind") = Range("Equity_wind").Value 'sets assumptions back to equity case
Range("tariff") = "Equity"
Application.ScreenUpdating = True 'turns screen updating back on


End Sub


Sub Proj1_OptA() 'for Tranche A debt sizing

For n = 1 To 3
Range("proj1_Afroz2").Formula = Range("proj1_Afroz1").Value
Range("proj1_A2").GoalSeek goal:=0, changingcell:=Range("proj1_A1")
Range("DSRA_paste").Value = Range("DSRA_copy").Value
Range("proj1_dsrA2").Formula = Range("proj1_dsrA1").Value
Range("levcap2").GoalSeek goal:=0, changingcell:=Range("levcap1")
Next n

End Sub

Sub Proj1_OptB() ' for Tranche B debt sizing

For n = 1 To 10

Range("proj1_Bfroz2").Formula = Range("proj1_Bfroz1").Value
Range("proj1_B2").GoalSeek goal:=0, changingcell:=Range("proj1_B1")
Range("proj1_dsrA2").Formula = Range("proj1_dsrA1").Value

Next n

End Sub

Sub Mezzanine_Opt()

For n = 1 To 10

Range("mezz_froz2").Formula = Range("Mezz_froz1").Value
Range("mezz1_a2").GoalSeek goal:=0, changingcell:=Range("mezz1_a1") ' does not feed into DSRA
Range("Mezzcap2").GoalSeek goal:=0, changingcell:=Range("Mezzcap1")
Next n

End Sub

RE: VBA Speed Issues: 2003 to 2007

Hi Ben, thanks for your query. I really hope you used the Timer function in VBA to time the code, and not a stopwatch....

There are three things I'd suggest doing to speed things up:

Check your variable declarations (these have subtly changed between versions, make sure you have as few Variants as possible)
Worksheet formatting (make sure every empty cell is formatted as "General". If it isn't, your file size will rocket)
Turn off automatic recalculation, then turn it back on again at the end. You don't need Excel to render data changes on the screen when it is calculating.

Copy/Paste operations are a big no-no as well. Set the value one cell to be equal to that of another, don't go via the clipboard. Finally, 2007 is a "bigger" application than 2003 is simply takes more processing power. Hence, with some legacy operations, you do see some speed differences despite it being the more up to date application!

Hope this helps,

Anthony

RE: VBA Speed Issues: 2003 to 2007

Didn't even know about the timer - will investigate! Thanks.

When you refer to automatic recalculation do you mean screen updating or something different? It is necessary for the Spreadsheet to run through its calculations, but the user doesnt need to see what's happening.(I'm hoping i've already enabled this feature)

When I say copy paste I mean:
Range(X).Value = Range(Y).Value

I'm hoping that's not the clipboard intensive one. Regardless what is the code to empty the clipboard?

AS to filesize its not a huge sheet c. 3mb in 2003, 1mb in 2007. I was hoping that smaller filesize = quicker operation.

Cheers - Ben

RE: VBA Speed Issues: 2003 to 2007

Hi Ben. Your "copy paste" is correct. When beginners use the .select, .copy and .paste methods it really slows the code down. I don't mean screen updating, I mean the actual calculation of the code on the worksheet. You can set it to manual with:

Application.Calculation = xlManual

and then turn it back on again at the end:

Application.Calculation = xlAutomatic

Smaller filesize does not necessarily mean faster operation. The code might be doing something complicated that locks the machine up. Try running the code on different machines too, it might be a machine-specific problem.

Hope this helps,

Anthony

Tue 25 Jan 2011: Automatically marked as resolved.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Counting Non Number Cells (Text)

If you try to use the COUNT FUNCTION =COUNT(Cell range)with a range of cells with numbers and or containing text fields you wil find that that the text cells will be excluded from the the count. If you want to include them try the the COUNTA FUNCTION =COUNTA(Cell range). This counts both text and number cell values.

View all Excel hints and tips


Server loaded in 0.08 secs.