Excel VBA - Auto move rows not working correctly

drewhx15

New Member
Joined
Jun 6, 2019
Messages
31
Hello,

I am hoping you can help.

The below code works on and off. It is driving me mad as it intermittently stops doing the task. I have two sheets (1 & 2) -

When column M (Sheet1) has one of 4 outcomes added (Drop down list), it then moves it from Sheet1 to Sheet2 automatically, problem is it repeatedly stops working after a few successful moves.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
A = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

For I = 2 To A

If Worksheets("Sheet1").Cells(I, 13).Value = "Successful" Or Worksheets("Sheet1").Cells(I, 13).Value = "Un-Successful" Or Worksheets("Sheet1").Cells(I, 13).Value = "N/A" Or Worksheets("Sheet1").Cells(I, 13).Value = "Other" Then
Worksheets("Sheet1").Rows(I).Cut
Worksheets("Sheet2").Activate

B = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet2").Cells(B + 1, 1).Select

ActiveSheet.Paste

Worksheets("Sheet1").Activate

End If

Next

For I = 2 To A

If Worksheets("Sheet1").Cells(1, 1).Value = " " Then
Rows(I).Delete

End If

Next

End Sub

Really hope you legends can help me with thism I have been trying for days but, now realise I need to ask the experts for help :)

Drew
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Column = 13 Then
      Select Case Target.Value
         Case "Successful", "Un-Successful", "N/A", "Other"
            Target.EntireRow.Copy Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
            Target.EntireRow.Delete
         Case ""
            Target.EntireRow.Delete
      End Select
   End If
End Sub
 
Upvote 0
Hi Fluff,

Thanks for your response! The code moves from sheet1 to sheet2 but, it is overwriting the last row it moved instead of continuing down from the next available row? Hope that makes sense?

Kind regards,
Drew
 
Upvote 0
Will column A always have a value, or can it be blank?
 
Upvote 0
Hi, I didn't notice but column A was empty :/ I have populated the sheet and it is moving them correctly :) - Thank you so much for your quick and AWESOME support. I love this forum! Thanks again <3
 
Upvote 0
In case that ever happens again you can use
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Column = 13 Then
      Select Case Target.Value
         Case "successful", "Un-Successful", "N/A", "Other"
            Target.EntireRow.Copy Sheets("Sheet2").Range("M" & Rows.Count).End(xlUp).Offset(1, -12)
            Target.EntireRow.Delete
         Case ""
            Target.EntireRow.Delete
      End Select
   End If
End Sub
which uses col M to find the last row.
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,822
Messages
6,121,772
Members
449,049
Latest member
greyangel23

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