VBA array - find largest element
I'm sure that the function is working fine. I've obviously got something in my outer nesting layers which led to the failure of the
function in my application. I have a pretty good sized procedure and I know that if I post it, then I would need to explain some
things and then I would be subject to all kinds of suggestions and/or criticisms regarding my code. Since I'm still working
feverishly on this project and need to get it done, I don't think I'll post the code this time. I do appreciate everybody for taking
an interest in this little snippet of my project. I hope I will be so lucky again when I have a better challenge. As a matter of
fact, I think I've got a good one now, which I'll post seperately in just a few minutes.
Thanks again!
Richard
--
RMC,CPA
"Postman" wrote in message ...
Could you stick an example sheet somewhere so we can see it not working?
If Z = Application.Max(prog) isn't working right it's a worry.
P
"R. Choate" wrote in message
...
| Perhaps something in the outer nesting of my code has prevented the simple
solution from working. Believe me, I wanted it to work
| and I tried it and I got the wrong answer. I know of you and I have great
respect for your Excel knowledge. All I can say is that
| there is something about my situation that is preventing the one line of
code from working. Now that 2nd bit of code you gave me
| below does work and is an improvement over what I wrote. However, remember
that when I wrote it, I had been trying everything to get
| the right answer. I went to the NG as a last resort, mostly because I do
have experience with this and I should be able to write the
| appropriate code for something so basic without going to the NG for an
answer. Sometimes that approach leads to overly cumbersome
| code as you have pointed out.
|
| I do not have a large number of elements, either. Nowhere close to 5461.
Again, I like that 2nd 6 lines of code you gave me and it
| works, but the one line of code
|
| Z = Application.Max(prog)
|
| does not give me the right answer.
|
| Hopefully I can save your patience and willingness to help me for another
question later.
|
| Thanks, Tom
|
| Richard
| --
| RMC,CPA
|
|
| "Tom Ogilvy" wrote in message
...
| From an academic standpoint, there is nothing your code does that couldn't
| be done in one line with the Max function by replaceing *All* your code
with
|
| Z = Application.Max(prog)
|
| Unless your array exceeds 5461 elements and you are using xl2000 or
earlier.
|
|
| Using your approach you have made it unnecessarily complex. If the max
will
| always be greater than zero then
|
| Z = 0
| for i = lbound(prog) to ubound(prog)
| if prog(i) Z then
| Z = prog(i)
| end if
| Next
|
| Would be the same.
|
| --
| Regards,
| Tom Ogilvy
|
|
| "R. Choate" wrote in message
| ...
| I appreciate the attempts, but it did not work in my situation. Perhaps
it
| had something to do with the dynamic resizing of the
| arrays or something else that was in the more complicated actual code
than
| what I gave in my example because I wanted to simplify
| the specific problem and not sound confusing. In any case, I solved the
| issue so I won't bother you guys any further on my post. It
| is academic at this point.
|
| Thanks again,
| --
| RMC,CPA
|
| "R. Choate" wrote:
|
| No, Norman's code does not work and the index number code indicated
| incorrect syntax in the editor. If I have an array where
| Prog(1)
| = 15, Prog(2) = 0, and Prog(3) = 2, Norman's code returns the "2"
| associated with Prog(3). I need for the code to return the "15"
| after looping through all of them and determining that 15 was the
| largest element in the array. Here is the solution I finally
| worked out on my own (Using option base 1):
|
| High = 0
| For A = 2 To BoxNums
|
| If Prog(A) - 0
| High Then
| High =
| Prog(A)
| Else
| GoTo
| goaroundagain
| End If
|
| goaroundagain:
|
| Next
| If High = 0
Then
| Z =
Prog(1)
| + High
| ElseIf High <
| Prog(1) Then
| Z =
Prog(1)
| Else
| Z = High
| End If
| --
| RMC,CPA
|
|
|
|
|