0

I am trying to split a string in excel and then writing each element of the string into one column of another sheet but i cant seem to finish looping before it gets a subscript out of range error.

Sub S1()

Dim Wb As Workbook
Dim rowValue() As String
Dim i As Variant

For i = 2 To 15500
With Worksheets(1)
    Value2 = Worksheets(1).Cells(i, 1)
    rowValue = Split(Worksheets(1).Cells(i, 1).Value, " ")
    rowValue(11) = rowValue(11) & " " & rowValue(12) & " " & rowValue(13)
    arraySize = UBound(rowValue) - LBound(rowValue) + 1
    If arraySize > 3 Then
        For x = 0 To arraySize
            'Place the split values into 1 column each
        Worksheets("Sheet2").Cells(i, x + 1).Value = rowValue(x)

        Next x
    Else
        'do nothing
    End If
End With
Next i

Above is my code, there are about 15500 rows in the first sheet. Everything works fine from rows 1-29 but after that I got the run-time error 9, subscript out of range.

Anyone knows why the loop stops after 29 rows?

6
  • 1
    show your complete code including variable declaration. In which line error pops up? Commented Nov 22, 2017 at 7:00
  • 3
    That kind of depends: we can't see what's on row 29, or any sample data for that matter! Commented Nov 22, 2017 at 7:00
  • Updated the code. The error lies with the line -> Worksheets("Sheet2").Cells(i, x + 1).Value = rowValue(x) Commented Nov 22, 2017 at 7:03
  • Worksheet 2 is just an empty sheet. Commented Nov 22, 2017 at 7:03
  • 2
    I think the value in row 29 doesn't have a long string in which you have 13 words separated by space.When the code stops right click on rowValue and click on Add Watch and check how many values are there in array. Commented Nov 22, 2017 at 7:05

3 Answers 3

2

Here is your new code:

Sub S1()
    Dim i As Integer, arr_Split() As String
    For i = 2 To 15500
        arr_Split = Split(Sheets(1).Cells(i, 1), " ")
        Range(Sheets(1).Cells(i, 2), Sheets(1).Cells(i, 2 + UBound(arr_Split))) = arr_Split
    Next i
End Sub

Try in out in place of the entire sub. Excel users often do things the hard way instead of checking if there are built-in features already created for the same task.

Some of the problems with your original subroutine:

  • You declare Wb as a Workbook but do not use it.

  • You don't declare Value2 but you use it.

  • You don't declare arraySize but you use it.

  • You don't declare x but you use it.

  • You use a With/End With Statement for no reason (since you don't refer to . between With and End With).

  • The With/End With Statement begins & ends 14,998 times. (See below.)

  • You hard code 11,12,13, which can be a problem unless all original values are 9 words long.

  • If (for example) your array has 5 items, arraySize will calculate as 3. (If an array has 5 items, they are numbered 0 to 4, and your formula calculates arraySize as 4 minus 1.

  • Your If arraysize>3 line will be incorrect due to errors above.

  • You're using a Variant (i) to count strictly integers.

  • The For/Next loop is using improper values for x.

  • If X=0 then Cells(i, x + 1) is incorrect since it starts writing at column 1 (replacing the source text).

  • Else 'do nothing is unneeded since Else statements are optional.

  • Row numbers are hard coded, meaning that you will always process exactly 14,998 rows.

  • There is no End Sub. (a copy/paste error I assume)

  • You're not using Option Explicit (which is why you didn't notice undeclared variables).

  • There was a couple more things which I am forgetting at the moment. Give me a moment to list all the things wrong with your code... :)


I left out these lines since (without sample data) it's unclear what they do. If I knew what they did, I am positive there's a better way to code the (possibly without hard-coded values).

  • rowValue(11) = rowValue(11) & " " & rowValue(12) & " " & rowValue(13)

  • If arraySize > 3 Then

Add them back in if needed, but make sure the variables are declared and that there is no way the hard-coded values will cause a problems can cause a problem with any possible data you have or will ever have.

Sign up to request clarification or add additional context in comments.

1 Comment

what happened to rowValue(11) = rowValue(11) & " " & rowValue(12) & " " & rowValue(13)?
1

This is a problem:

For x = 0 To arraySize

An array of size 10 (and lower bound=0) has an upper bound of 9, so there would be (eg) no rowValue(10)

Reworked with bounds checking:

Sub S1()

    Dim Wb As Workbook, rowValue, i As Long, ub As Long

    For i = 2 To 15500

        rowValue = Split(Worksheets(1).Cells(i, 1).Value, " ")
        ub = UBound(rowValue)

        If ub >= 13 Then
            rowValue(11) = rowValue(11) & " " & rowValue(12) & " " & rowValue(13)
        End If

        If ub > 2 Then
            Worksheets("Sheet2").Cells(i, 1).Resize(1, ub + 1).Value = rowValue
        End If

    Next i

End Sub

2 Comments

Definitely will cause the error. Now the big question is ... why doesn't it crash on row 2 ?!? (Maybe rows 2 to 29 only contain 2 or fewer spaces??)
No - it can't be less than 3 spaces, or else the line setting position 11 would crash.
0

use On Error Resume Next

Sub S1()

    Dim Wb As Workbook
    Dim rowValue() As String
    Dim i As Variant

    For i = 2 To 15500
        With Worksheets(1)
            Value2 = Worksheets(1).Cells(i, 1)
            On Error Resume Next ' USE THIS
            rowValue = Split(Worksheets(1).Cells(i, 1).Value, " ")
            rowValue(11) = rowValue(11) & " " & rowValue(12) & " " & rowValue(13)
            arraySize = UBound(rowValue) - LBound(rowValue) + 1
            If arraySize > 3 Then
                For x = 0 To arraySize
                    'Place the split values into 1 column each
                Worksheets("Sheet2").Cells(i, x + 1).Value = rowValue(x)

                Next x
            Else
                'do nothing
            End If
            On Error GoTo 0  ' USE THIS
        End With
    Next i

End Sub

3 Comments

It is fine, hiding an anticipated error, when the code itself is not factored well and no naming convention is followed.
But the error isn't anticipated, otherwise the OP wouldn't be asking the question.
"Don't worry about the easily-fixable errors in your code"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.