Problems using arrays (1 Viewer)

Zedster

Registered User.
Local time
Today, 09:03
Joined
Jul 2, 2019
Messages
168
I have never really used arrays to any degrees. I am trying to create a function to split a string by tabs, but I am struggling to get it to work even with spaces (not even tried tabs yet). I keep getting a type mismatch compile error but I can't see why. Can any advise?

Code:
Public Function SplitStringByTab(strInput As String) As String()

10    On Error GoTo err_SplitStringByTab

          Dim arrOutput() As String
          Dim i As Integer
          'use the split function
20        arrOutput = Split(strInput)
30        For i = LBound(arrOutput) To UBound(arrOutput)
40            Debug.Print i & " = " & arrOutput(i)
50        Next i

60        SplitStringByTab = arrOutput
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:03
Joined
May 7, 2009
Messages
19,229
you did not specify the Tab (vbTab) on your Split function:

20 arrOutput = Split(strInput, vbTab)

or space

20 arrOutput = Split(strInput, " ")

EDIT:

use Variant as your Return from the function:

Public Function SplitStringByTab(strInput As String) As Variant
 
Last edited:

Zedster

Registered User.
Local time
Today, 09:03
Joined
Jul 2, 2019
Messages
168
you did not specify the Tab (vbTab) on your Split function:

20 arrOutput = Split(strInput, vbTab)

or space

20 arrOutput = Split(strInput, " ")
As I said, I haven't even progressed to tabs yet, I can't even get it running with spaces. I understood the second argument was optional and if omitted it defaults to space, hence I left it off. I have tried adding , " " after strInput but I still get a compile error.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:03
Joined
May 7, 2009
Messages
19,229
i edited my post (#2), you need to Return a Variant.
run the test sub:
Code:
Public Function SplitStringByTab(strInput As String) As Variant

10    On Error GoTo err_SplitStringByTab

          Dim arrOutput() As String
          Dim i As Integer
          'use the split function
20        arrOutput = Split(strInput)
30        For i = LBound(arrOutput) To UBound(arrOutput)
40            Debug.Print i & " = " & arrOutput(i)
50        Next i

60        SplitStringByTab = arrOutput
err_SplitStringByTab:
End Function

Private Sub test()
Dim v As Variant
Dim i As Integer
v = SplitStringByTab("arnel puzon")
If IsArray(v) Then
    For i = LBound(v) To UBound(v)
        Debug.Print v(i)
    Next
End If
End Sub
 

Zedster

Registered User.
Local time
Today, 09:03
Joined
Jul 2, 2019
Messages
168
i edited my post (#2), you need to Return a Variant.
Thanks that gets me further it now runs but I am getting a runtime error 13 type mismatch, but it isn't triggering my error trapping routine! It gets to line 60 then goes to 70.

Code:
Public Function SplitStringByTab(strInput As String) As Variant

10    On Error GoTo err_SplitStringByTab

          Dim arrOutput() As String
          Dim i As Integer
          'use the split function
20        arrOutput = Split(strInput, " ")
30        For i = LBound(arrOutput) To UBound(arrOutput)
40            Debug.Print i & " = " & arrOutput(i)
50        Next i

60        SplitStringByTab = arrOutput
          
exit_SplitStringByTab:
70        Exit Function
          
err_SplitStringByTab:

80        MsgBox Err.Number & " " & Err.Description & vbCr & vbCr & "Error Line: " & Erl
90        Call Logger("Error", "meetings.basFunctions.SplitStringByTab," & Erl, Err.Number, Err.Description)
100       Resume exit_SplitStringByTab

End Function
 

Zedster

Registered User.
Local time
Today, 09:03
Joined
Jul 2, 2019
Messages
168
i edited my post (#2), you need to Return a Variant.
run the test sub:
Code:
Public Function SplitStringByTab(strInput As String) As Variant

10    On Error GoTo err_SplitStringByTab

          Dim arrOutput() As String
          Dim i As Integer
          'use the split function
20        arrOutput = Split(strInput)
30        For i = LBound(arrOutput) To UBound(arrOutput)
40            Debug.Print i & " = " & arrOutput(i)
50        Next i

60        SplitStringByTab = arrOutput
err_SplitStringByTab:
End Function

Private Sub test()
Dim v As Variant
Dim i As Integer
v = SplitStringByTab("arnel puzon")
If IsArray(v) Then
    For i = LBound(v) To UBound(v)
        Debug.Print v(i)
    Next
End If
End Sub

I am calling it from the immediate window using ?splitstringbytab ("This is a test string")
 

Gasman

Enthusiastic Amateur
Local time
Today, 09:03
Joined
Sep 21, 2011
Messages
14,223
You need to choose what you are returning?, one of the elements perhaps?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:03
Joined
May 7, 2009
Messages
19,229
you can't call it in immediate window, since the return is an Array().
create a test sub and call the function from there.
 

Zedster

Registered User.
Local time
Today, 09:03
Joined
Jul 2, 2019
Messages
168
Managed to get the principal working changed it to a sub. I will raise a separate issue on that with an appropriate title. Thanks for the help
 

Users who are viewing this thread

Top Bottom