LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
Ed Ed is offline
external usenet poster
 
Posts: 46
Default Min of 2 variables

I was using the if else, but below is easier.

Thanks again,

Ed English

c = Application.WorksheetFunction.Min(a, b)


On 20 Nov 2006 08:10:11 -0800, "John Coleman"
wrote:

Another possibilty is:

If a < b Then
c = a
Else
c = b
End If

Another thread today (on VLOOKUPS) disscussed the overhead in calling
worksheet functions from VBA. This made me curious about which method
of computing a min would be quicker. If you run the following 2 subs:

Sub Min1()
Randomize
Dim a As Double, b As Double, c As Double
Dim start As Double, elapsed As Double
Dim i As Long

start = Timer
For i = 1 To 1000000
a = Rnd()
b = Rnd()
c = Application.WorksheetFunction.Min(a, b)
Next i
elapsed = Timer - start
MsgBox elapsed
End Sub

Sub Min2()
Randomize
Dim a As Double, b As Double, c As Double
Dim start As Double, elapsed As Double
Dim i As Long

start = Timer
For i = 1 To 1000000
a = Rnd()
b = Rnd()
If a < b Then
c = a
Else
c = b
End If
Next i
elapsed = Timer - start
MsgBox elapsed
End Sub

You should find the second noticably faster than the first. On my
machine Min2() takes about half a second and min1() takes about 5
seconds. So - if your min calculation is in the body of a loop you
might want to use a straight VBA approach. On the other hand, if you
are using it only a few times in your code then the readability of the
worksheetfunction.min approach might win out. YMMV.

HTH

-John Coleman

Ed wrote:
What is the code to obtain the variable c as the minimum of variables
a and b?

c = minimum of a and b

 
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
Not at all clear on use of variables and/or object variables JMay-Rke Excel Discussion (Misc queries) 11 July 4th 08 06:36 PM
Using variables in a name Vispy Excel Discussion (Misc queries) 4 February 22nd 06 01:17 AM
What are my variables ... ? DJB[_5_] Excel Programming 4 July 4th 05 03:51 PM
SUM IF and two variables Leigh Ann Excel Worksheet Functions 6 May 25th 05 03:24 AM
Variables Tim U[_2_] Excel Programming 2 June 22nd 04 10:12 PM


All times are GMT +1. The time now is 10:32 AM.

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"