Subscript out of range

Ossian13

New Member
Joined
Oct 21, 2016
Messages
46
Hi Guys,

im trying to copy rows from one sheet to another based on their rating, Rating is in column E (Long,medium or short) and i have 3 sheets with each name.
I have a list with movies and based on their rating i want the entire row to be copied on the specific sheet.

here is my code.

Code:
Sub Lup2()


Dim FilmLength As Integer, Filmrating As String, LastRow As Long, i As Integer, film As Workbook


Application.ScreenUpdating = False




Set film = ActiveWorkbook


LastRow = film.Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row


    For i = 2 To LastRow
   ' MsgBox film.Worksheets("Sheet1").Range("D" & i).Value
    If film.Worksheets("Sheet1").Range("D" & i).Value < 150 And film.Worksheets("Sheet1").Range("D" & i) > 99 Then
    film.Worksheets("Sheet1").Range("E" & i).Value = "Medium"
    Else
    
    If film.Worksheets("Sheet1").Range("D" & i).Value < 100 Then
    film.Worksheets("Sheet1").Range("E" & i).Value = "Short"
    Else
    film.Worksheets("Sheet1").Range("E" & i).Value = "Long"
    
    
    End If
    End If
    
    Range("A2").Activate
    Range(ActiveCell, ActiveCell.End(xlToRight)).Copy
    Worksheets(Filmrating).Activate
    
    'ActiveWorkbook.Worksheets(Filmrating).Range("A1").Activate
    Selection.PasteSpecial
    
    Next i
    
Application.ScreenUpdating = True


End Sub

i get the error from the title of the thread and this row is highlighted ,,Worksheets(Filmrating).Activate,,
Do you have any idea how should i fix this?

Thank you and regards,
Ossian
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
.
Place Filmrating in quotes. "Filmrating"
 
Upvote 0
but that means i have a sheet called Filmrating, and i don't. I only have Sheet1 which contains all the movies, then i have 3 more sheets, Long, Medium and Short.
I tried anyways and i have the same result.
 
Upvote 0
Filmrating is declared as a string but never set therefore it is an empty string
 
Upvote 0
Assuming that Sheet1 has headers in row 1, try the following:
Code:
Sub CopyRows()
    Dim FilmLength As Integer, LastRow As Long, i As Long
    Application.ScreenUpdating = False
    LastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
     For i = 2 To LastRow
        If Sheets("Sheet1").Range("D" & i).Value < 150 And Sheets("Sheet1").Range("D" & i) > 99 Then
            Sheets("Sheet1").Range("E" & i).Value = "Medium"
        ElseIf Sheets("Sheet1").Range("D" & i).Value < 100 Then
            Sheets("Sheet1").Range("E" & i).Value = "Short"
        Else
            Sheets("Sheet1").Range("E" & i).Value = "Long"
        End If
     Next i
     Sheets("Sheet1").Range("E1:E" & LastRow).AutoFilter Field:=1, Criteria1:="Short"
     Sheets("Sheet1").Range("E2:E" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("Short").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
     Sheets("Sheet1").Range("E1:E" & LastRow).AutoFilter Field:=1, Criteria1:="Medium"
     Sheets("Sheet1").Range("E2:E" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("Medium").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
     Sheets("Sheet1").Range("E1:E" & LastRow).AutoFilter Field:=1, Criteria1:="Long"
     Sheets("Sheet1").Range("E2:E" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Copy Sheets("Long").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
     If Sheets("Sheet1").AutoFilterMode = True Then Sheets("Sheet1").AutoFilterMode = False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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