Re: Runtime error 9 - subscript out of range error

VbaHell

Well-known Member
Joined
Jan 30, 2011
Messages
1,220
Re: Runtime error 9 - subscript out of range error

Hello All

I hope you can help on this please

Re: Runtime error 9 - subscript out of range error on the code below that is bold

[Lastrow = Sheets("data").Cells(Rows.Count, "I").End(xlUp).Row
For i = 2 To Lastrow

If Cells(i, "J").Value = "D" Then Cells(i, 1).Resize(, 8).Copy Sheets(Cells(i, "I").Value).Cells(Sheets(Cells(i, "I").Value).Cells(Rows.Count, "K").End(xlUp).Row + 1, "K")

If Cells(i, "J").Value = "C" Then Cells(i, 1).Resize(, 8).Copy Sheets(Cells(i, "I").Value).Cells(Sheets(Cells(i, "I").Value).Cells(Rows.Count, "B").End(xlUp).Row + 1, "B")

Next]
 
Re: Runtime error 9 - subscript out of range error

Hello VbaHell,

From what I understand what you want to do, I believe this should it.

Code:
Sub Populate()


    Dim DstWks  As Worksheet
    Dim i       As Long
    Dim Lastrow As Long
    Dim SrcWks  As Worksheet
    Dim vArray  As Variant
    Dim WksName As Variant
                    
        vArray = Array("August 18", "July 18", "June 18", "May 18", "April 18", "March 18", "February 18", "January 18", "December 17", _
                        "November 17", "October 17", "September 17", "August 17", "July 17")
                        
        Set SrcWks = Sheets("data")
            Lastrow = SrcWks.Cells(Rows.Count, "B").End(xlUp).Row
        
            SrcWks.Range("K1").FormulaR1C1 = "Text Date"
            SrcWks.Range("K2:K" & Lastrow).FormulaR1C1 = "=TEXT(RC[-2],""mmmm yy"")"
    
            SrcWks.Range("K2:K" & Lastrow).Value = SrcWks.Columns("I2:I" & Lastrow).Value
            
        For Each WksName In vArray
            Set DstWks = Worksheets(WksName)
            For i = 2 To Lastrow
                If SrcWks.Cells(i, "J").Value = "D" Then SrcWks.Cells(i, 1).Resize(, 8).Copy DstWks.Cells(Rows.Count, "K").End(xlUp).Offset(1, 0)
                If SrcWks.Cells(i, "J").Value = "C" Then SrcWks.Cells(i, 1).Resize(, 8).Copy DstWks.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
            Next i
        Next WksName
     
        Call Formats
    
End Sub
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Re: Runtime error 9 - subscript out of range error

Hi Leath Thank you very much for helping me on this, I will check this out at the weekend as I am away at the moment
I must admit I was loosing the will to live with this post.
 
Upvote 0
Re: Runtime error 9 - subscript out of range error

Hello VbaHell,

The macro assumes all of the sheets in the array exist in the workbook. If that is not the case you can use this amended version to handle the errors...

Macro with Error Handling
Code:
Sub Populate()


    Dim DstWks  As Worksheet
    Dim i       As Long
    Dim Lastrow As Long
    Dim SrcWks  As Worksheet
    Dim vArray  As Variant
    Dim WksName As Variant
                    
        vArray = Array("August 18", "July 18", "June 18", "May 18", "April 18", "March 18", "February 18", "January 18", "December 17", _
                        "November 17", "October 17", "September 17", "August 17", "July 17")
                        
        Set SrcWks = Sheets("data")
            Lastrow = SrcWks.Cells(Rows.Count, "B").End(xlUp).Row
        
            SrcWks.Range("K1").FormulaR1C1 = "Text Date"
            SrcWks.Range("K2:K" & Lastrow).FormulaR1C1 = "=TEXT(RC[-2],""mmmm yy"")"
    
            SrcWks.Range("K2:K" & Lastrow).Value = SrcWks.Columns("I2:I" & Lastrow).Value
            
        For Each WksName In vArray
            On Error Resume Next
                Set DstWks = Worksheets(WksName)
                If Not Err Then
                    For i = 2 To Lastrow
                        If SrcWks.Cells(i, "J").Value = "D" Then SrcWks.Cells(i, 1).Resize(, 8).Copy DstWks.Cells(Rows.Count, "K").End(xlUp).Offset(1, 0)
                        If SrcWks.Cells(i, "J").Value = "C" Then SrcWks.Cells(i, 1).Resize(, 8).Copy DstWks.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
                    Next i
                Else
                    MsgBox "The Worksheet """ & WksName & """ was Not Found.", vbExclamation
                End If
            On Error GoTo 0
        Next WksName
     
        Call Formats
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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