Re: Max/Min Functions
- From: "Greg Maxey" <gmaxey@xxxxxxxxxxxxxxxxxxx>
- Date: Sat, 3 Dec 2005 10:57:19 -0500
Jonathan,
I solved my immediate problem with the below code. I was searching Google
and found the following tip that Jezebel had posted in response to a
similiar question:
"Put your arguments into an array, and pass that as a single argument. "
I knew I had the array and so I removed the "()" following vArray in the
Function you provided
This only confirms my near complete lack of understanding of 1) Arrays, 2)
Passing arguments. I really don't understand the basic concept and usually
simply stumble on a workable solution. If you (or any other benevolent
soul) have time, I would certainly appreciate it if you could provide a
sample of how you intended your code to be employed with an explanation of
how it works. Of all the thing is VBA, it is those empty parens "()" that
confound me the most. I feel that I am close to an epiphany but I could use
divine intervention. Thanks for everything
Sub CallMacro()
Dim myArray
Dim oMaxValue As Long
Dim oMinValue As Long
Dim i As Long
myArray = Array(3, 1, 13)
oMaxValue = MaxOfArray(myArray)
oMinValue = MinOfArray(myArray)
MsgBox oMaxValue
MsgBox oMinValue
End Sub
Function MaxOfArray(vArray As Variant) As Long
Dim iStart As Long
Dim iEnd As Long
Dim vMax As Long
Dim i As Long
iStart = LBound(vArray)
iEnd = UBound(vArray)
vMax = vArray(iStart)
For i = iStart + 1 To iEnd
If vArray(i) > vMax Then vMax = vArray(i)
Next i
MaxOfArray = vMax
End Function
Function MinOfArray(vArray As Variant) As Long
Dim iStart As Long
Dim iEnd As Long
Dim vMin As Long
Dim i As Long
iStart = LBound(vArray)
iEnd = UBound(vArray)
vMin = vArray(iStart)
For i = iStart + 1 To iEnd
If vArray(i) < vMin Then vMin = vArray(i)
Next i
MinOfArray = vMin
End Function
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
Jonathan West wrote:
> Hi Greg,
>
> Ii go along with Jezebel's comment regarding the efficiency of your
> comparison. Getting stuff onto a single line is not necessarily going
> to make things run faster.
>
> As for getting a max from an array, something like this should work
>
> Function MaxOfArray(vArray() as Variant) As Variant
> Dim iStart as Long
> Dim iEnd as Long
> Dim vMax as Variant
> Dim i As Long
>
> iStart = LBound(vArray)
> iEnd = UBound(vArray)
> vMax = vArray(iStart)
> For i = iStart + 1 to iEnd
> If vArray(i) > vMax Then vMax = vArray(i)
> Next i
> MaxOfArray = vMax
> End Function
>
> You can adapt the code as needed depending on what kind of items you
> are comparing. If they are all integers for instance, you can speed
> things up a bit by using Long instead of variant everywhere.
>
> You should also have no problem working out the Min equivalent of the
> function
>
>
> "Greg Maxey" <gmaxey@xxxxxxxxxxxxxxxxxxx> wrote in message
> news:%237m8Y579FHA.2320@xxxxxxxxxxxxxxxxxxxxxxx
>> I put together the following little snippet of code to determine
>> Max/Min values with two arguments. I think it was JGM that posted
>> the min function a while back and I just reversed the equality signs
>> to make the max function work:
>>
>> Sub Test()
>> Dim i As Long
>> i = min(1, 1000)
>> MsgBox i
>> i = max(5, 500)
>> MsgBox i
>> End Sub
>> Private Function min(a As Long, b As Long)
>> min = -((a < b) * a + (a >= b) * b)
>> End Function
>> Private Function max(a As Long, b As Long)
>> max = -((a > b) * a + (a <= b) * b)
>> End Function
>>
>> Does anyone know if someone has figure out how to determine max or
>> min values in an array using Word VBA? Thanks.
>>
>> --
>> Greg Maxey/Word MVP
>> See:
>> http://gregmaxey.mvps.org/word_tips.htm
>> For some helpful tips using Word.
.
- Follow-Ups:
- Re: Max/Min Functions
- From: Jay Freedman
- Re: Max/Min Functions
- From: Jezebel
- Re: Max/Min Functions
- From: Jonathan West
- Re: Max/Min Functions
- References:
- Max/Min Functions
- From: Greg Maxey
- Re: Max/Min Functions
- From: Jonathan West
- Max/Min Functions
- Prev by Date: Re: Run-time error '4605'
- Next by Date: Re: How to add a List Box to a existing macro
- Previous by thread: Re: Max/Min Functions
- Next by thread: Re: Max/Min Functions
- Index(es):
Relevant Pages
|
Loading