VBA to move a row to another sheet based on criteria

survivalbloke

New Member
Joined
Jul 29, 2016
Messages
9
Hello. First off, I apologize for having to ask this because I'm certain that the solution has already been posted elsewhere in this community, but I'm actually so inept in Excel, I'm not entirely sure I would recognize the solution if I saw it, so I'm asking. If based on my question, you know where a thread is which has solved this, please let me know!

I would like a way to automatically move an entire row to another sheet based on the contents of a specific cell in a row. Here's my(extremely simplified) example:


AB
ITEMSTATUS
1item 1for sale
2item 2sold

<tbody>
</tbody>

In the above example, we will call that sheet "Inventory", and the second sheet(not shown) will be called "Sold". What I'd like is a way to automatically move any column which has a "status" of "sold". In the above example, row 2 should be moved to the sheet named "Sold". I'm hoping that is clear, but if for some reason it isn't, I'll explain what I'm doing. I've got a spreadsheet(which was made awesome thanks to the folks here) I use to track inventory. When I get inventory, I enter it in the "Inventory" sheet of the workbook. Once something is sold and no longer available, I cut and paste that row from the "Inventory" sheet to the "Sold" sheet.

Also, if this solution is based on VBA, how does one execute a VBA script?

Thanks!
 
Assuming you want to us Sold or Cancelled

And always copy to sheet named Sold
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  8/28/2018  10:48:41 AM  EDT
If Not Intersect(Target, Range("B:B")) Is Nothing Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Dim Lastrow As Long
Lastrow = Sheets("Sold").Cells(Rows.Count, "B").End(xlUp).Row + 1
If Target.Value = "Sold" Or Target.Value = "Cancelled" Then
Rows(Target.Row).Copy Destination:=Sheets("Sold").Rows(Lastrow)
Rows(Target.Row).Delete
End If
End If
End Sub

Or do you mean if Sold copy to sheet named Sold and if Cancelled copy to sheet names Cancelled

BOTH Sold and Cancelled going to the same sheet, so the top one should work. Thanks for the help!
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
So I'm assuming you mean my last post worked for you:
Glad I was able to help you.
Come back here to Mr. Excel next time you need additional assistance.
 
Upvote 0
I used your code above and it worked great.
If I could ask one question?
I have my data in a table on the target sheet, but it copies it to the row below the table.
I then have to go to the target sheet and drag the table down over the new row.

Is there a way to make it copy the data into the table?
 
Upvote 0
The term "Table" is used very loosely on this forum by a lot of Users. A Excel "Table" is a Range on a Excel Sheet.

Any time you use the term "Table" you need to provide the Table name.

Like "Table1" or any other name you may give your Table

Or if your referring to a sheet you need to provide the sheet name.
 
Upvote 0
OK thanks for the heads up.
I am trying to get the row moved to a sheet called "Members paid (2019)" and into a table on that sheet called "Table1"
 
Upvote 0
Survival.

I discovered one mistake in my script.
Try this script instead:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Version 2
If Not Intersect(Target, Range("B:B")) Is Nothing Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Dim Lastrow As Long
Lastrow = Sheets("Sold").Cells(Rows.Count, "B").End(xlUp).Row + 1

If Target.Value = "Sold" Then
Rows(Target.Row).Copy Destination:=Sheets("Sold").Rows(Lastrow)
Rows(Target.Row).Delete
End If
End If
End Sub

have tried this code and it is not working for some reason? Weird part is if I change the name of the second sheet from sold I get a run time error '9': subscript out of range. But if I change the page back to Sold it does nothing.
I
 
Upvote 0
Survival.

I discovered one mistake in my script.
Try this script instead:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Version 2
If Not Intersect(Target, Range("B:B")) Is Nothing Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Dim Lastrow As Long
Lastrow = Sheets("Sold").Cells(Rows.Count, "B").End(xlUp).Row + 1

If Target.Value = "Sold" Then
Rows(Target.Row).Copy Destination:=Sheets("Sold").Rows(Lastrow)
Rows(Target.Row).Delete
End If
End If
End Sub
Is there a way to automate this even further?

Right now, our employees would enter a date say in column A (using the existing example code). Column B then uses an If(isblank) formula to populate whether either Open or Resolved. Upon them entering a date in A, B will say Resolved. Once they enter the date and column B populates to Resolved, I want it to move to Sheet2 without the employee having to actually go into the cell in Column B and hit enter.

Hopefully that all makes sense.
 
Upvote 0
Is there a way to automate this even further?

Right now, our employees would enter a date say in column A (using the existing example code). Column B then uses an If(isblank) formula to populate whether either Open or Resolved. Upon them entering a date in A, B will say Resolved. Once they enter the date and column B populates to Resolved, I want it to move to Sheet2 without the employee having to actually go into the cell in Column B and hit enter.

Hopefully that all makes sense.
I suggest you start a new posting and be very specific what your attempting to do.
This post is more then 5 years old.
 
Upvote 0

Forum statistics

Threads
1,216,082
Messages
6,128,713
Members
449,464
Latest member
againofsoul

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