Re: Max/Min Functions

Tech-Archive recommends: Fix windows errors by optimizing your registry



Jezebel,

Thanks. Yes I see the simplicity of the multi-line approach. As you might
have read my reply to Jonathan you are aware that I am very inexperienced,
practically clueless, with arrays and passing things. I am sure that your
code snippet would work, I just don't have the skills to plug it in ;-).

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
"Jezebel" <warcrimes@xxxxxxxxxxxxxx> wrote in message
news:%23U1R4S99FHA.3312@xxxxxxxxxxxxxxxxxxxxxxx
> You can use code along these lines --
>
> pMax = srcArray(lbound(srcArray))
>
> For pIndex = lbound(srcArray) To ubound(srcArray) - 1
> pValue = Max(srcArray(pIndex), srcArray(pIndex + 1))
> If pValue > pMax Then
> pMax = pValue
> End If
> Next
>
>
> As a separate issue (although relevant if you're testing large arrays)
> your min/max functions, notwithstanding their one-line elegance, are
> actually rather inefficient because they involve two comparisons and three
> arithmetic operations. Quicker (use GetTickCount to measure it) is the
> simple --
>
> If a > b Then
> Max = a
> Else
> Max = b
> End If
>
>
>
>
>
> "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.
>>
>
>


.



Relevant Pages

  • Max/Min Functions
    ... Sub Test() ... MsgBox i ... Private Function min ... values in an array using Word VBA? ...
    (microsoft.public.word.vba.general)
  • Re: Max/Min Functions
    ... As a separate issue (although relevant if you're testing large arrays) your ... > MsgBox i ... > Private Function min ... > values in an array using Word VBA? ...
    (microsoft.public.word.vba.general)
  • Re: Max/Min Functions
    ... >> Sub Test() ... >> MsgBox i ... >> Private Function min ... >> Greg Maxey/Word MVP ...
    (microsoft.public.word.vba.general)
  • Re: Array Pointer
    ... On the msgbox() line, I get runtime error 9, subscript out of range. ... Dim arr As Variant ... LBound and UBound are used to read the lower and upper bounds of arrays whose ... If flag = 1 Then ...
    (microsoft.public.excel.programming)
  • Re: Extracting unique entries and assigning it to a named range
    ... The obvious advantage is has arrays for keys and items. ... Private Function SetRangeAs Range ... Set SetRange = Worksheets.Add.Range ... > Dim target As Range ...
    (microsoft.public.excel.programming)