Hi there,
I have a pretty simple macro in my excel file that runs a few basic goal seeks. Unfortunately, this code has a tendency to crash frequently, especially when the user's inputs are too off and/or when the result is difficult or impossible to find for Excel. Usually what happens is that the macro starts running and crashes Excel. I've tried adding an "EnableCancelKey = xlErrorHandler" to give a way out for users when the macro crashes but it does not work every time.
Private Sub Goal_Seek()
Dim T As Long
Application.EnableCancelKey = xlErrorHandler
Application.ScreenUpdating = False
On Error GoTo errHandler
T = Range("purchase_price")
Range("COC_value").GoalSeek Goal:=Range("COC_target"), ChangingCell:=Range("purchase_price")
Range("COC_result") = Range("purchase_price")
Range("CF_value").GoalSeek Goal:=Range("CF_target"), ChangingCell:=Range("purchase_price")
Range("CF_result") = Range("purchase_price")
Range("IRR_value").GoalSeek Goal:=Range("IRR_target"), ChangingCell:=Range("purchase_price")
Range("IRR_result") = Range("purchase_price")
Range("MIRR_value").GoalSeek Goal:=Range("MIRR_target"), ChangingCell:=Range("purchase_price")
Range("MIRR_result") = Range("purchase_price")
Range("purchase_price") = T
Application.EnableCancelKey = xlInterupt
Application.ScreenUpdating = True
exitHandler:
Application.EnableCancelKey = xlInterupt
Application.ScreenUpdating = True
Exit Sub
errHandler:
If Err.Number = 18 Then
Range("purchase_price") = T
Resume exitHandler
Else
MsgBox "The Maximum Offering Price tool can't run properly under the proposed acquisition and operating parameters. Please review them and make the necessary adjustments before running the tool again."
Range("purchase_price") = T
Resume exitHandler
End If
End Sub
To give some context, the macro is trying to assess the maximum purchase price based on certain targeted return indicators (COC, CF, IRR, MIRR) provided by the user.
So my questions:
- Is there a more "solid" approach to achieve the same result without using .GoalSeek?
- What could I do to make that code less subject to crash? How can I better handle crashes when they occur?
- Anything else I could do to improve the code?
Thanks!
Bookmarks