VBA runtime Error object 424 required

CalumJames

New Member
Joined
Jun 18, 2011
Messages
13
Ok, so I've created the below VBA to move rows however it seems to work some of the time, but then others it throws a runtime 424 error.

Does anyone have any idea why?


Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 11 Then
If Target = "Completed Minors" Then
Application.EnableEvents = False
nxtRow = Sheets("Completed Minors").Range("K" & Rows.Count).End(xlUp).Row + 1
Target.EntireRow.Copy _
Destination:=Sheets("Completed Minors").Range("A" & nxtRow)
Target.EntireRow.Delete
End If
If Target = "Completed Majors" Then
Application.EnableEvents = False
nxtRow = Sheets("Completed Majors").Range("K" & Rows.Count).End(xlUp).Row + 1
Target.EntireRow.Copy _
Destination:=Sheets("Completed Majors").Range("A" & nxtRow)
Target.EntireRow.Delete
End If
If Target = "Completed Other Sites" Then
Application.EnableEvents = False
nxtRow = Sheets("Completed Other Sites").Range("K" & Rows.Count).End(xlUp).Row + 1
Target.EntireRow.Copy _
Destination:=Sheets("Completed Other Sites").Range("A" & nxtRow)
Target.EntireRow.Delete
End If
If Target = "Minor Transitions" Then
Application.EnableEvents = False
nxtRow = Sheets("Minor Transitions").Range("K" & Rows.Count).End(xlUp).Row + 1
Target.EntireRow.Copy _
Destination:=Sheets("Minor Transitions").Range("A" & nxtRow)
Target.EntireRow.Delete
End If
End If
Application.EnableEvents = True
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
If any of the criteria are matched you are deleting Target along with the rest of the row it's in.

So when the code comes to the next one, there is no Target to work with.

Assuming it's Target is going to match one of the terms you could try a Select Case structure instead of all the If/End Ifs
Code:
Select Case Target.Value
 
        Case "Completed Minors" 
 
            Application.EnableEvents = False
            nxtRow = Sheets("Completed Minors").Range("K" & Rows.Count).End(xlUp).Row + 1
            Target.EntireRow.Copy _
                    Destination:=Sheets("Completed Minors").Range("A" & nxtRow)
            Target.EntireRow.Delete

         Case "Completed Majors" 

             Application.EnableEvents = False
             nxtRow = Sheets("Completed Minors").Range("K" & Rows.Count).End(xlUp).Row + 1
             Target.EntireRow.Copy _
                     Destination:=Sheets("Completed Minors").Range("A" & nxtRow)
             Target.EntireRow.Delete
....

End Select
That will make sure there is only ever one row deleted, if it there's a match of course.
 
Upvote 0

Forum statistics

Threads
1,224,561
Messages
6,179,521
Members
452,923
Latest member
JackiG

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