VBA Code Debug Help

Siegmier

New Member
Joined
Jul 29, 2014
Messages
8
OK so I have a code where if there is a S in a column it will delete the row of the S and move the row over to another sheet, then if there is a D in the column it will simply delete the row indicated, but once I inserted the D part I am getting an error where if I put an S in the column I then get a run time error after it completes the if then. I also get the same error when I insert a line.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False_
If Target.Column = 15 And Target.Value = "S" Then
'targets column O and if there is an S then
Rows(Target.Row).Copy _
Destination:=Worksheets("Shipped").Rows(Worksheets("Shipped") _
.UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1)
Rows(Target.Row).Delete shift:=xlUp
End If
'Copy the row with the S paste it in one above the last used cell. Then delete said row and shift data up
If Target.Column = 15 And Target.Value = "D" Then
'targets column O and if there is an S then
Rows(Target.Row).Delete shift:=xlUp
End If
Application.EnableEvents = True
End Sub



the row that highlights when I debug it is the "If Target.Column = 15 And Target.Value = "D" Then" when it goes through the S script, but it highlights the S line when I insert a line.

Any help is much apriciated

-Sieg

EDIT: The runtime error is 424 if that helps
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
OK so using an ElseIf statement I got rid of the error when moving the line with S, however when I insert a line it still gives me runtime error 13

New Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False_
If Target.Column = 15 And Target.Value = "S" Then <------This line is highlighted in debugger
'targets column O and if there is an S then
Rows(Target.Row).Copy _
Destination:=Worksheets("Shipped").Rows(Worksheets("Shipped") _
.UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1)
Rows(Target.Row).Delete shift:=xlUp
ElseIf Target.Column = 15 And Target.Value = "D" Then
'targets column O and if there is a D then
Rows(Target.Row).Delete shift:=xlUp
'Delete the row containing the D
End If
'Copy the row with the S paste it in one above the last used cell. Then delete said row and shift data up
Application.EnableEvents = True
End Sub
 
Last edited:
Upvote 0
@Neon, I made it bold, underlined it, and added a note to the side to point it out :-)

PS the column you need to put the letter in is the 15th aka O, and I am currently getting the error only when I insert a line, otherwise it is now working fine(after I adjusted the if statement to an elseif)
 
Upvote 0
You can't compare an array (Target.Value, when you've changed more then one cell, like by inserting a row) to a single value. That's what's causing the runtime error.

If this only needs to work for single-cell changes, then

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    With Target
        If .Cells.Count = 1 And .Column = 15 Then
            On Error GoTo Oops
            Application.EnableEvents = False

            
            If .Value = "S" Then
                .EntireRow.Copy Worksheets("Shipped").Cells(Rows.Count, "A").End(xlUp)(2)
                .EntireRow.Delete
            ElseIf .Value = "D" Then
                .EntireRow.Delete
            End If
Oops:
            Application.EnableEvents = True
        End If
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,222,827
Messages
6,168,482
Members
452,192
Latest member
FengXue

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