Convert String array to Long array

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,806
Office Version
  1. 2016
Platform
  1. Windows
Code:
Sub test()

    Dim s As String
    Dim Ar() As String
    
    s = "1,2,3,4,5"
    ar = Split(s, ",")
    MsgBox WorksheetFunction.Max(ar)

End Sub
The above Split Function returns a String array - Is there a way to convert this String array to a Long array so that we can obtain the Max element (ie: 5) but w/o looping though the array and converting each element one by one ?
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Code:
Sub test()

    Dim s As String
    Dim Ar() As String
    dim arval() as long
    
    s = "1,2,3,4,5"
    ar = Split(s, ",")

    For row= 0 to ubound(ar)-1
        arval(row)=val(ar(row))
    next row

    MsgBox WorksheetFunction.Max(arval)

End Sub
 
Upvote 0
Perhaps you missed that Jaafar was asking if this could be done without looping to convert each value? ;)
 
Upvote 0
yeh your right. lol so is there? i dont know that you can...
 
Upvote 0
I do not know of any way without a loop, that is, no way that would be faster and/or more efficient than a loop (in memory).

but w/o looping though the array and converting each element one by one ?

That takes milliseconds on any decent PC, no? Even with large arrays. Why bother?
 
Upvote 0
Or:
Code:
s = "1,2,3,4,5"
MsgBox Evaluate("MAX(" & s & ")")
 
Upvote 0
Mike & Rory's suggestions are neat and produce the result without looping as requested. I'm not sure what Jaafar's ultimate goal is (he often seems to try things to just see if he can :)) but I would contend that looping through the array is faster than either of those methods anyway. Of course if this is not being repeated a lot of times the speed difference is unlikely to matter.

I must say that I didn't test on any other (eg longer) strings so I'm not sure how much difference that would make. However this code would be even faster if the max value was not the last value in the string.

Code:
s = "1,2,3,4,5"
Ar = Split(s, ",")
Mx = Ar(0)
For j = 0 To UBound(Ar)
    If Ar(j) > Mx Then Mx = Ar(j)
Next j
MsgBox Mx
 
Upvote 0
Hi all,

Maybe I'm missing something, but in Mike and Rory's, 's' is not an array.

Mark
 
Upvote 0

Forum statistics

Threads
1,224,565
Messages
6,179,549
Members
452,927
Latest member
rows and columns

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top