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

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

c = minimum of a and b
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 269
Default Min of 2 variables

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

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


  #3   Report Post  
Posted to microsoft.public.excel.programming
Ed Ed is offline
external usenet poster
 
Posts: 46
Default Min of 2 variables

Thank you sir.

Ed English




On 20 Nov 2006 06:14:47 -0800, "Zone" wrote:

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

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

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Min of 2 variables

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


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Min of 2 variables

Somewhat strangely, the following sub takes about twice as long as
min2(). I was expecting it to be quicker (or at least no slower). IIf
is a VBA function (which I have never used before) that evaluates its
first argument and, if true, returns the second argument otherwise it
returns the third argument. Thus it is the equivalen to C's ? operator.
I though that the compiler might be able to do some optimization with
it:

Sub Min3()
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 = IIf(a < b, a, b)
Next i
elapsed = Timer - start
MsgBox elapsed
End Sub


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




  #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

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
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 02:53 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"