Excel VBA Run-time error '9' Subscript out of range

gsingh4

New Member
Joined
Jun 3, 2016
Messages
10
I am not very knowledgeable in coding. Just a marketing student trying to do some research.
I borrowed this code from the internet to collect data on movies. When I run the function, it shows me error 9 - subscript out of range (in bold - year = parts1(2)). I tried looking for answers and fiddled around but all in vain.
Any help will really be appreciated.


Option Explicit


Dim adjRev As Double
Dim rev As Double
Dim tickets As Long
Dim year As String
Dim rating As String
Dim genre As String
Dim director As String
Dim daysIn As Integer
Dim sDaysIn As String
Dim budnum As Double
Dim adjBud As Double
Dim budget As String
Dim runtime As String
Dim minutes As Integer
Dim x As Long
Dim y As Integer
Dim z As Integer
Dim address As String
Dim dist As String
Dim row As Long
Dim endRow As Long
Dim d As Date
Dim day As String
Dim date1 As String
Dim parts1() As String
Dim hours As Integer
Dim parts2() As String
Dim parts4() As String
Dim parsed As String
Dim page As String
Dim temp As String
Dim distParts() As String
Dim genreParts() As String
Dim ratingParts() As String
Dim budParts() As String
Dim tempDate As String
Dim dateParts() As String
Dim itemDate As String
Dim closeDay As Date
Dim monthNum As Integer
Dim startDay As Date
Dim tempParts() As String




Sub FillVariables()


Format
Scrape
Calculate


End Sub


Sub Format()

'Format all of the columns to the kind we need
Sheets("Movies A-Z").Activate
endRow = ActiveSheet.UsedRange.Rows.Count
ActiveSheet.Columns("A:O").NumberFormat = "General"
ActiveSheet.Columns(3).NumberFormat = "$#,##"
ActiveSheet.Columns(4).NumberFormat = "$#,##"
ActiveSheet.Columns(12).NumberFormat = "$#,##"
ActiveSheet.Columns(13).NumberFormat = "$#,##"
ActiveSheet.Columns(5).NumberFormat = "mm/dd/yyyy"
ActiveSheet.Range("A1:O1").NumberFormat = "General"


'Deletes all rows that start off with "n/a" in them - they're likely to have too little info
For row = 2 To endRow
For x = 1 To 5
If Cells(row, x).Value = "n/a" Then
Cells(row, x).EntireRow.Delete
x = 1
End If
DoEvents
Next
Next


endRow = ActiveSheet.UsedRange.Rows.Count


'Finds the day of the week and year that movies were released on
For row = 2 To endRow


d = Cells(row, 5).Value
day = WeekdayName(Weekday(d))


Cells(row, 11).Value = day


date1 = Cells(row, 5).Value
parts1 = Split(date1, "/")
year = parts1(2)
monthNum = parts1(0)

Cells(row, 15).Value = monthNum
Cells(row, 6).Value = year


Next

End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hello,

In my opinion it's a problem with a date format. Please check if dates in E column have proper format. If they actually have 2 slashes. Split function has to split dates into 3 element array. However if there is an error and date will be split in 2 or it won't be split at all you will get an error on:
Code:
[B]year = parts1(2)[/B]
because 3rd element doesn't exist in this array.
 
Upvote 0
gsingh4,

Welcome to the messageboard.

The reason you are getting the error is because there is no such value in your string....

Consider this example and then review your data set:

BEFORE:
Excel 2010
ABCD
parts1parts1(0)parts(1)parts(2)
6/2015

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]
[TD="align: right"]6/1/2016[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]3[/TD]
[TD="align: right"]6/2/2016[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]4[/TD]
[TD="align: right"]6/3/2016[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]5[/TD]
[TD="align: right"]6/4/2016[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]6[/TD]
[TD="align: right"]6/5/2016[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]7[/TD]
[TD="align: right"]6/6/2016[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

[TD="align: center"]8[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

</tbody>
Sheet4



AFTER:
Excel 2010
ABCD
parts1parts1(0)parts(1)parts(2)
6/2015ERROR - Because there is no second "/"

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]
[TD="align: right"]6/1/2016[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]2016[/TD]

[TD="align: center"]3[/TD]
[TD="align: right"]6/2/2016[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]2016[/TD]

[TD="align: center"]4[/TD]
[TD="align: right"]6/3/2016[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]2016[/TD]

[TD="align: center"]5[/TD]
[TD="align: right"]6/4/2016[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]2016[/TD]

[TD="align: center"]6[/TD]
[TD="align: right"]6/5/2016[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]2016[/TD]

[TD="align: center"]7[/TD]
[TD="align: right"]6/6/2016[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2016[/TD]

[TD="align: center"]8[/TD]

[TD="align: right"]6[/TD]
[TD="align: right"]2015[/TD]

</tbody>
Sheet4



Code:
[COLOR=#0000ff]Sub[/COLOR] Test()

[COLOR=#0000ff]    Dim[/COLOR] x [COLOR=#0000ff]As Integer[/COLOR]
[COLOR=#0000ff]    Dim[/COLOR] date1 [COLOR=#0000ff]As String[/COLOR]
  [COLOR=#0000ff]  Dim [/COLOR]parts1 [COLOR=#0000ff]As Variant[/COLOR]

    For x = 2 To 9[COLOR=#008000] 'Rows 2 to 9[/COLOR]
    
        date1 = Cells(x, 1).Value
        parts1 = Split(date1, "/")
        
        Cells(x, 2) = parts1(0) [COLOR=#008000]'Column B Month Value[/COLOR]
        Cells(x, 3) = parts1(1) [COLOR=#008000]'Column C Day Value[/COLOR]
        Cells(x, 4) = parts1(2) [COLOR=#008000]'Column D Year Value[/COLOR]
        
  [COLOR=#0000ff]  Next[/COLOR] x

[COLOR=#0000ff]End Sub[/COLOR]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top