Visual Basic/Hey Aaron,
I'm still getting...
Expert: Aaron Young - 5/27/2005
QuestionHey Aaron,
I'm still getting the same error...
strTextLines = SplitEx(strReadText, vbNewLine)
Compile error: "Can't assign to array"
I don't know...I'm about to give up.
-------------------------
Followup To
Question -
Hey Aaron,
I asked a question at VBForums, and instead of
typing it again, here is the link...
http://www.vbforums.com/showthread.php?p=2027435#post2027435
I hope you can help me, as this is driving me crazy!
Thanks,
Ron
Answer -
Try:
'-------------------------------------------------------
Private Function SplitEx(ByVal sString As String, Optional ByVal sDelim As String = " ") As Variant
Dim sItem As String
Dim sList() As String
Dim lChar As Long
Dim lCount As Long
Do
' Reset Split for non-Quoted Values
lChar = InStr(sString, sDelim)
If lChar > 0 Then
' Extract the Item from the String
sItem = Left(sString, lChar - 1)
' Remove Item from the Remaining String
sString = Mid(sString, lChar + Len(sDelim))
Else
' Remaining String, becomes final Item
sItem = sString
sString = ""
End If
' If something was extracted, add it to the Array
If Len(sItem) > 0 Then
ReDim Preserve sList(lCount)
sList(lCount) = sItem
lCount = lCount + 1
End If
' Continue until no more delimiters are found.
Loop While lChar > 0
' If there were items, return the array
If lCount > 0 Then
SplitEx = sList
Else
' Otherwise return an empty array
SplitEx = Array()
End If
End Function
Private Sub Command1_Click()
Dim intFree As Integer
Dim strReadText As String
Dim strTextLines() As String
intFree = FreeFile
Open "C:\SomeFile.txt" For Binary As #intFree
strReadText = Space$(LOF(intFree))
Get #intFree, , strReadText
Close #intFree
' Create array of text lines from the file...
strTextLines = SplitEx(strReadText, vbNewLine)
Text1.Text = strTextLines(0) ' First line...
Text2.Text = strTextLines(1) ' Second line...
End Sub
'-------------------------------------------------------
Regards,
- Aaron.
AnswerTry changing "strTextLines" to a Variant data type instead of a String array, i.e.
'----------------------------------------------------------
Private Sub Command1_Click()
Dim intFree As Integer
Dim strReadText As String
'Dim strTextLines() As String
Dim strTextLines As Variant
intFree = FreeFile
Open "C:\SomeFile.txt" For Binary As #intFree
strReadText = Space$(LOF(intFree))
Get #intFree, , strReadText
Close #intFree
' Create array of text lines from the file...
strTextLines = SplitEx(strReadText, vbNewLine)
Text1.Text = strTextLines(0) ' First line...
Text2.Text = strTextLines(1) ' Second line...
End Sub
'----------------------------------------------------------
Regards,
- Aaron.