Array Max

M

Macroman

I have a string it looks like this

myString = "99,100,45,91,125,65,11"

I then turn this into an array using split function.

intArray = split(myString , ",")

Thus

intArray(0) = 99
intArray(1) = 100 etc

How do I idnetify the max value within the intArray ?

I tried

intMax = Max(intArray) ,

and it didnt work, would I need to loop from 0 to number of entried in my
array and code that way ?
I was hoping for a single line of code, such a my poor example.

thanks

Macroman
 
H

Harlan Grove

...
...
How do I idnetify the max value within the intArray ?

I tried

intMax = Max(intArray) ,

Use

intMax = Application.WorksheetFunction.Max(intArray)
 
A

Alan Beban

I don't know what version Harlan Grove is using; his suggestion returns
0 for me.

I believe the problem is that the elements of intArray as posted are
strings. If noone else posts a solution in the next half hour or so, I
will post one.

Alan Beban
 
F

Frank Kabel

Hi
something like the following works for me:
Sub foo()
Dim mystring As String
Dim intarray
Dim intarray_2() As Integer
Dim maxvalue
Dim i As Integer
mystring = "99,100,45,91,125,65,11"
intarray = Split(mystring, ",")
ReDim intarray_2(UBound(intarray))
For i = 0 To UBound(intarray)
intarray_2(i) = CInt(intarray(i))
Next
maxvalue = Application.WorksheetFunction.Max(intarray_2)
MsgBox maxvalue
End Sub
 
B

Bob Phillips

Joe said:
UBound(IntArray) + 1 gives the number of elements in the array.

Not necessarily, it depends upon the LBound. The only certain count is

UBound(IntArray) - LBound(IntArray) + 1
 
A

Alan Beban

Macroman said:
I have a string it looks like this

myString = "99,100,45,91,125,65,11"

I then turn this into an array using split function.

intArray = split(myString , ","). . . .

How do I idnetify the max value within the intArray ?

One way, if the functions in the freely downloadable file at
http://home.pacbell.net/beban are available to your workbook

Sub test3001()
Dim myString As String, intArray() As Integer
myString = "99,100,45,91,125,65,11"
Assign Split(myString, ","), intArray
intMax = Application.Max(intArray)
End Sub

Alan Beban
 
H

Harlan Grove

I don't know what version Harlan Grove is using; his suggestion returns
0 for me.
...

I'm running XL8/97 at the moment, so didn't bother trying to use Split since VBA
5.x doesn't provide it. If the OP has an array of integers, which I admit I just
assumed, then Application.WorksheetFunction.Max works just fine. If the OP
starts with a comma-delimited string, then the version-neutral way to deal with
this is

intMax = Evaluate("MAX({" & MyString & "})")

the trick being to treat MyString an array constant, thus avoiding the limit on
the number of arguments that can be fed to MAX(). For a generally string with a
given field delimiter string FS, there's always

intMax = Evaluate("MAX({" & _
Application.WorksheetFunction.Substitute(MyString, FS, ",") & _
"})")

If the OP has a delimited string and wants the array of integers as well as the
max, the former could be given by

intArray = Evaluate("{" & Application.WorksheetFunction.Substitute(MyString, _
FS, ",") & "}")

which is 1-based rather than 0-based which Split generates. Still, this
elininates the need to convert Split()'s result into integers.

So no need to exert yourself answering this one, Alan. No one's holding their
breath waiting for the complete answer you could (should?) have given in your
previous response. Or are you going to dazzle us with another pointless VBA
function?
 
B

Bob Phillips

Harlan,

You are an old grouch! You got it wrong and you haven't even got the grace
to just accept that<G>.

I know you won't be able to resist, but there is really no point in replying
with another pointless tirade at me<EBG>

Bob

Harlan Grove said:
...
..

I'm running XL8/97 at the moment, so didn't bother trying to use Split since VBA
5.x doesn't provide it. If the OP has an array of integers, which I admit I just
assumed, then Application.WorksheetFunction.Max works just fine. If the OP
starts with a comma-delimited string, then the version-neutral way to deal with
this is

intMax = Evaluate("MAX({" & MyString & "})")

the trick being to treat MyString an array constant, thus avoiding the limit on
the number of arguments that can be fed to MAX(). For a generally string with a
given field delimiter string FS, there's always

intMax = Evaluate("MAX({" & _
Application.WorksheetFunction.Substitute(MyString, FS, ",") & _
"})")

If the OP has a delimited string and wants the array of integers as well as the
max, the former could be given by

intArray = Evaluate("{" &
Application.WorksheetFunction.Substitute(MyString, _
 
H

Harlan Grove

Bob Phillips said:
You are an old grouch!
True

. . . You got it wrong and you haven't even got the grace
to just accept that<G>.

I accept nothing.

I admit (again) that I missed the point that Split returns an array of
strings. However, there are better (as in simpler, faster, less memory
usage, less version dependent) ways of parsing text strings into arrays of
numbers than calling user-defined functions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top