Help using split()

reneuend

Board Regular
Joined
May 20, 2009
Messages
155
I created the following function but I get a mismatch error. Does the array have to be dimensioned to the right size first? :confused:

Code:
Function TestArray() As Variant()
    myData = "1,2,3"
    TestArray = Split(myData, ",")
End Function

Thanks!
 
Last edited:

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
You're not telling the Split function everything it needs. You need to tell it WHAT to split on.

For example:

Split(myData,",")(0) should yield 1
Split(myData,",")(1) should yield 2
Split(myData,",")(2) should yield 3

Haven't tested it, but perhaps this is what you're after:

Code:
Function TestArray() As Variant()
    myData = "1,2,3"
    TestArray = Split(myData[B][COLOR=red],","[/COLOR][/B])
End Function
 
Upvote 0
This worked for me:

Code:
Function testarray() As Variant
    Dim myData As String
    myData = "1,2,3"
    testarray = Split(myData, ",")
End Function
 
Sub Test()
    MsgBox testarray(0)
End Sub
 
Upvote 0
Sorry! I caught that issue late and fixed it in the thread, but I'm still getting a mismatch error in Excel 2007.

I found that if I set the function return as "String()" and not "Variant()" it works. I'm a bit surprised...does anyone know why this is?

I just saw your post Andrew. Thanks! I guess I don't need the parenthesis in the return declaration. variant instead of variant().
 
Last edited:
Upvote 0
Yes! It worked perfectly. I had two issues that made this confusing for me. I appreciate everyone's input on this!

Cheers! ;)
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

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