Home » excel » excel – Split function not creating array()

excel – Split function not creating array()

Posted by: admin May 14, 2020 Leave a comment


I’ve worked this code over a few times, but I always seem to have a problem with the Split statement passing my array. Here’s a piece of code that’s not working and I can’t figure out why

Private Sub parseCSV()
'Parse "Notes" column and return Moods/Keywords to their apropriate cells

    Dim CSV As String
    Dim fullArray() As String
    Dim lRow As Long
    Dim Keywords As String
    Dim Moods As String

    Dim i As Long

    lRow = ActiveSheet().Range("BL" & ActiveSheet().Rows.Count).End(xlUp).Row

    For i = 3 To lRow

        CSV = ActiveSheet.Range("BL" & i)
        fullArray() = Split(CSV, Chr(10))

        ActiveSheet.Range("CE" & i).Value = fullArray(3)
        ActiveSheet.Range("CD" & i).Value = fullArray(2)

    Next i

End Sub

It gives an Err-9, value out of range. It’s so weird. I’ve tested it a bunch of different ways and this is what I’ve found.

It will return the 1st piece of data in the array. I can change my delimiter to any thing i want and get exactly what I would expect in the first place of the array returned, if I change my code to fullArray(). I’ve also tried using Application.Index(fullArray, 1, 2), no dice. I’ve tried using various combination of array coordinates and variant/string combos. Nothing. Please help, I would greatly appreciate it.

Here is an example of what is contained in the cell being split;

To License Contact, [email protected], 212.661.6990/310.247.8630
Go, White, Know
Happy, Rock, Upbeat, Catchy
How to&Answers:

You need to test the UBound first to verify there are actually 3 lines. A UBound(fullArray) of 2 indicates three lines. This is an example of a zero based array. The third line ends up in fullArray(2).

If your input is three lines long you can get the split using:


Here is your adjusted code:

For i = 3 To lRow

    If UBound(fullArray) >= 2 Then
        CSV = ActiveSheet.Range("BL" & i)
        fullArray() = Split(CSV, Chr(10))

        ActiveSheet.Range("CE" & i).Value = fullArray(2) 'enter third line
        ActiveSheet.Range("CD" & i).Value = fullArray(1) 'enter second line
        'this will output the cell address of any cell that
        'doesn't have 3 lines within the cell
        Debug.Print ActiveSheet.Range("BL" & i).Address
    End If

Next i