Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default More Efficient If function

I need some help improving the efficiency of an if statement in VBA. I can
do a nested if statement in excel and the formula takes no time to change
when an input is changed. I need to add more conditions so I am at the
extent of my nested if skills. The function is:

Function MertonA(IsCall, IsAmerican, Price, Exercise, u, d, n, RowCount,
ColumnCount)

If IsCall = True And IsAmerican = True Then
Formula 1
ElseIf IsCall = False And IsAmerican = True Then
Formula2
ElseIf IsCall = True And IsAmerican = False Then
Formula3
ElseIf IsCall = False And IsAmerican = False Then
Formula4
End If

End Function

Each formula is about 2 lines so I omitted it for brevity. The function is
pasted in a 100 x 100 range and takes about 20 seconds to update completely.

Help would be appreciated.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default More Efficient If function

Following is the form your function might take.

Note that all of the variables are declared;
as there were no declarations they were all variants.
Also the function has been given a data type (String)
A Select Case statement is used to replace the If/End If structure.
You can use as many case conditions as necessary.
In addition, the result is assigned to the function itself ...

Function MertonA(ByRef IsCall As Boolean, ByRef IsAmerican As Boolean, _
ByRef Price As Double, ByRef Exercise As Boolean, ByRef u As Double, _
ByRef d As Double, ByRef n As Double, ByRef RowCount As Long, _
ByRef ColumnCount As Long) As String

Select Case True
Case IsCall = True And IsAmerican = True
MertonA = Formula1
Case IsCall = False And IsAmerican = True
MertonA = Formula2
Case IsCall = True And IsAmerican = False
MertonA = Formula3
Case IsCall = False And IsAmerican = False
MertonA = Formula4
Case IsCall
MertonA = moreformulas
Case Else
MertonA = alternate
End Select

End Function

--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"will12985"

wrote in message
I need some help improving the efficiency of an if statement in VBA. I can
do a nested if statement in excel and the formula takes no time to change
when an input is changed. I need to add more conditions so I am at the
extent of my nested if skills. The function is:

Function MertonA(IsCall, IsAmerican, Price, Exercise, u, d, n, RowCount,
ColumnCount)

If IsCall = True And IsAmerican = True Then
Formula 1
ElseIf IsCall = False And IsAmerican = True Then
Formula2
ElseIf IsCall = True And IsAmerican = False Then
Formula3
ElseIf IsCall = False And IsAmerican = False Then
Formula4
End If

End Function

Each formula is about 2 lines so I omitted it for brevity. The function is
pasted in a 100 x 100 range and takes about 20 seconds to update completely.

Help would be appreciated.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default More Efficient If function

I wouldn't say this is more efficient, but it more easy to see where you're
going:

Function MertonA(IsCall, IsAmerican, Price, Exercise, u, d, n, RowCount,
ColumnCount)
If IsCall = True Then
If IsAmerican = True Then
Formula1
Else
Formula3
End If
Else
If IsAmerican = True Then
Formula2
Else
Formula4
End If
End If
End Function

Depending on what other conditions you need to add, "Select Case" may be
useful.

NickHK

"will12985" wrote in message
...
I need some help improving the efficiency of an if statement in VBA. I

can
do a nested if statement in excel and the formula takes no time to change
when an input is changed. I need to add more conditions so I am at the
extent of my nested if skills. The function is:

Function MertonA(IsCall, IsAmerican, Price, Exercise, u, d, n, RowCount,
ColumnCount)

If IsCall = True And IsAmerican = True Then
Formula 1
ElseIf IsCall = False And IsAmerican = True Then
Formula2
ElseIf IsCall = True And IsAmerican = False Then
Formula3
ElseIf IsCall = False And IsAmerican = False Then
Formula4
End If

End Function

Each formula is about 2 lines so I omitted it for brevity. The function

is
pasted in a 100 x 100 range and takes about 20 seconds to update

completely.

Help would be appreciated.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 947
Default More Efficient If function

... I need to add more conditions so I am at the
extent of my nested if skills.


Hi. One technique when deciding what to do based on a group of Boolean
variables is to treat the group as a Binary number.

n = Abs(4 * IsOther + 2 * IsCall + IsAmerican)
Run "Formula" & n


This will generate the unique numbers 0,1,2,3,4...etc
If you can keep to your formula names like "Formula0",
"Formula1","Formula2", etc then this could be another option.
I included a third Binary variable called "IsOther."
Note that True is -1 in Vba, so we use Abs at the end.

'//Note:
'0=All False
'1=Only IsAmerican is True
'2=Only IsCall is True
'3=Both IsCall and IsAmerican are True
'...etc

As a side note in your original formula, some like to drop the comparison
operator (=True/False)
This is because your variables are Boolean to begin with. Maybe something
like this:
I've changed the formula names to match the above idea.

If IsCall And IsAmerican Then
Formula3
ElseIf Not IsCall And IsAmerican Then
Formula1
ElseIf IsCall And Not IsAmerican Then
Formula2
ElseIf Not IsCall And Not IsAmerican Then
Formula0
End If

--
HTH :)
Dana DeLouis
Windows XP & Office 2003


"will12985" wrote in message
...
I need some help improving the efficiency of an if statement in VBA. I can
do a nested if statement in excel and the formula takes no time to change
when an input is changed. I need to add more conditions so I am at the
extent of my nested if skills. The function is:

Function MertonA(IsCall, IsAmerican, Price, Exercise, u, d, n, RowCount,
ColumnCount)

If IsCall = True And IsAmerican = True Then
Formula 1
ElseIf IsCall = False And IsAmerican = True Then
Formula2
ElseIf IsCall = True And IsAmerican = False Then
Formula3
ElseIf IsCall = False And IsAmerican = False Then
Formula4
End If

End Function

Each formula is about 2 lines so I omitted it for brevity. The function
is
pasted in a 100 x 100 range and takes about 20 seconds to update
completely.

Help would be appreciated.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is more efficient Brad Excel Discussion (Misc queries) 2 November 20th 06 09:13 PM
more efficient VBA? markx Excel Programming 4 August 7th 06 05:41 PM
An efficient find function jdw[_2_] Excel Programming 0 August 23rd 04 05:44 PM
More efficient way? Steph[_3_] Excel Programming 6 June 23rd 04 09:34 PM
Which is more efficient? Norm[_5_] Excel Programming 3 April 2nd 04 04:24 PM


All times are GMT +1. The time now is 04:31 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"