|
-
Aug 1st, 2006, 05:05 AM
#1
return any variable type as array
as the computer i am using does not have VB and only has office 97 i decided i would like to write a replacement for the split function as it is not supported in vba5, however i am having a problem returning an array, i did a search here and the posts indicate this should work
VB Code:
Function split(str As String, c As String) as Variant ()
but it does not like it at all, i get expected end of statement error after variant, also i would like to be able to process any data type, how to be able to do that?
pete
-
Aug 1st, 2006, 05:07 AM
#2
Re: return any variable type as array
Try removing the parentisis (after Variant)
-
Aug 1st, 2006, 05:13 AM
#3
Re: return any variable type as array
This seemed to work (I renamed the deliniator):
VB Code:
Private Sub Form_Load()
Dim strArrBuff() As String
strArrBuff = split("a,b,c", ",")
MsgBox strArrBuff(0)
End Sub
Private Function split(str As String, strDeliniator As String) As Variant
'Do Split Stuff (strArr was my internal test Array)
split = strArr
End Function
-
Aug 1st, 2006, 05:20 AM
#4
Re: return any variable type as array
Also (found here): http://www.vbforums.com/showthread.p...ight=vb5+split
VB Code:
'VB5 Split Function
'split function for VB5 and under vb6 has the function
'posted originally by Aaron Young
Public Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant
Dim sParts() As String
Dim lParts As Long
Dim lPos As Long
lPos = InStr(sString, sSeparator)
While lPos
ReDim Preserve sParts(lParts)
sParts(lParts) = Left(sString, lPos - 1)
sString = Mid(sString, lPos + Len(sSeparator))
lPos = InStr(sString, sSeparator)
lParts = lParts + 1
Wend
If Len(sString) Then
ReDim Preserve sParts(lParts)
sParts(lParts) = sString
End If
Split2 = IIf(lParts, sParts, Array())
End Function
'Example:
Private Sub Form_Load()
Dim vLines As Variant
Dim lLine As Long
vLines = Split2("Line1,Line2,Line3", ",")
For lLine = 0 To UBound(vLines)
List1.AddItem vLines(lLine)
Next
End Sub
-
Aug 1st, 2006, 05:25 AM
#5
Re: return any variable type as array
There are many fast Splits here: http://www.xbeat.net/vbspeed/
-
Aug 1st, 2006, 05:34 AM
#6
Re: return any variable type as array
thnx bruce
that will work,
how about being able to split any data type?
-
Aug 1st, 2006, 05:39 AM
#7
Re: return any variable type as array
thnx all, i will follow up the other links
i guess really only strings need to be split anyway, this is the code i used
VB Code:
Function split(str As String, c As String) As Variant
Dim spl()
Dim n As Integer, pos As Integer
ReDim spl(10)
spl(n) = Mid(str, 1, InStr(1, str, c) - 1)
n = n + 1
Do
pos = InStr(pos + 1, str, c)
If Not pos = 0 And Not pos = Len(str) Then
spl(n) = Mid(str, pos + 1, InStr(pos + 1, str, c) - 1 - pos)
n = n + 1
If n = UBound(spl) Then ReDim Preserve spl(n + 10)
Else: Exit Do
End If
Loop
n = n - 1
ReDim Preserve spl(n)
split = spl
End Function
pete
-
Sep 20th, 2008, 03:19 AM
#8
Junior Member
Re: return any variable type as array
westconn1,
I am using vb5 as well and have been trying to get Split to work for hours breaking up a textbox full of commas.
Could you just run through this code for me so that I can understand how it works with respect to the size of the array and number of textboxes it uses. I am a little lost,
Cheers,
Snookered
-
Sep 20th, 2008, 05:58 AM
#9
Re: return any variable type as array
this seems like a long time ago, you can redim preserve the array in the loop by 1 more, each time (as in post #4), but it is more efficient to redim the array less often, you could just start with 100 or any other number (i started with 10), the most efficient is the number closest to the expected size of the array + a few, but as you don't know the final size you have to just take a stab at it based on instinct, the loop uses instr to find each delimiter and takes the string between each pair of delimiters and puts into the array,
it does not use any textboxes, just pass your textbox.text and the delimiter to the function
you should also make it so that if the delimiter is not found then the result is a 1 element array, i did not do that at that time
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 20th, 2008, 12:07 PM
#10
Re: return any variable type as array
I finally posted a separate CodeBank thread for the QuickSplit I've made. The code is uncommented, but it is very fast in what it does. With a change in one line you also can make it VB5 compatible. The line is at the very end of the code.
-
Sep 26th, 2008, 04:23 PM
#11
Junior Member
Re: return any variable type as array
I have this working fine. Thanks all for your help.
Snookered.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|