Automatically move rows from one table to another table based on cell data

Cgavenas

New Member
Joined
Aug 30, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have a workbook with three tables, all housed on separate sheets. The tables are named: "Open", "Won", & "Lost". (Sheets are named the same as their corresponding table)
I would like to automatically move the row from the Open table to either the Won table or the Lost table based on the status selected. In the column titled "Status", each row has a data validation drop-down selection of "Open", "Won" or "Lost". Once a row is selected as "Won" or "Lost" I would like it moved to the bottom of its corresponding table and the row to be deleted from the "Open" table.

I am new to VBA but from what I have gathered from googling/researching I believe this will need an Event Procedure VBA code for my "Open" sheet.

Please let me know if you need any other information.
Thanks in advance for your time!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Of course! Apologies I didn't add in the beginning.

Quote Log - BM.xlsm
ABCDEF
1RFQ NameCustomer NameFollow up DateAmountQuote Number(s)Status
2testcustomer test10/3/2023$45,345.001964654Open
3testcustomer test10/3/2023$103.001964654Open
4testcustomer test10/3/2023$5,373.001964654Open
5testcustomer test10/3/2023$7,878.001964654Open
6testcustomer test10/3/2023$54,613.001964654Open
7testcustomer test10/3/2023$9,823.001964654Open
8testcustomer test9/5/2023$1,000.001964654Open
9testcustomer test10/3/2023$1,000.001964654Open
10testcustomer test8/22/2023$100,000.001964654Open
11testcustomer test10/3/2023$5,620.001964654Open
12testcustomer test10/3/2023$16,558.001964654Open
13testcustomer test10/3/2023$7,894.001964654Open
14testcustomer test10/3/2023$45,678.001964654Open
15testcustomer test11/11/2023$53,751.001964654Open
16testcustomer test10/3/2023$2,456.001964654Open
17testcustomer test10/12/2023$7,878.001964654Open
18testcustomer test10/4/2023$783.001964654Open
19testcustomer test10/3/2023$8,678.001964654Open
20testcustomer test10/12/2023$248.001964654Open
21testcustomer test10/3/2023$7,867.001964654Open
22testcustomer test11/11/2023$562.001964654Open
23testcustomer test10/12/2023$8,956.001964654Open
24testcustomer test10/3/2023$16,546,547.001964654Open
25testcustomer test10/3/2023$3,858.001964654Open
26testcustomer test10/3/2023$1,852.001964654Open
27testcustomer test10/3/2023$15,236.001964654Open
28testcustomer test10/3/2023$965.001964654Open
29testcustomer test10/2/2023$1,234.001964654Open
30testcustomer test10/31/2023$6,541.001964654Open
Open
Cells with Data Validation
CellAllowCriteria
F2:F30ListOpen,Won,Lost


Quote Log - BM.xlsm
ABCDEFG
1RFQ NameCustomer NameFollow up DateAmountQuote Number(s)StatusSales Order
2
Won


Quote Log - BM.xlsm
ABCDEFG
1RFQ NameCustomer NameFollow up DateAmountQuote Number(s)StatusReason
2
Lost
 
Upvote 0
This might take a few goes to get right, but here's a starting point. Tables are always a bit tricky when it comes to formatting. Try the following in the sheet code area of the "Open" sheet (right click the sheet tab name, select View Code, paste the code in the window that appears on the right of screen). Save the file as macro-enabled, and try it on a copy of your file first.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.CountLarge = 1 And Not Intersect(Range("F:F"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        Dim s As String, i As Long
        s = Target.Value2
        If s <> "Open" Then
            Worksheets(s).ListObjects(s).ListRows.Add
            i = Worksheets(s).ListObjects(s).ListRows.Count
            Worksheets(s).ListObjects(s).DataBodyRange(i, 1).Resize(1, 6).Value = _
            Range("A" & Target.Row).Resize(1, 6).Value
            If Worksheets(s).ListObjects(s).DataBodyRange(1, 1) = "" Then _
            Worksheets(s).ListObjects(s).ListRows(1).Delete
            Me.ListObjects("Open").ListRows(Target.Row - 1).Delete
        End If
    End If
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub
 
Upvote 0
Hi kevin9999, apologies for the delay in responding.
I tested various options with the code you sent, and it works perfectly!
Thank you for the help and your time!

I did do some formatting to give each sheet a facelift after the fact but when I added in a few columns/rows I think I threw things off.
I updated the column range "F:F" to "G:G" and changed the Value Range "A" to "B" but when I test the code after that it will move the row to the appropriate table, but it will delete a different row.

Do you mind reviewing the below and seeing if you can help me update based on my new data?
Apologies in advance for the back and forth!

Quote Log - BM Test.xlsm
ABCDEFGH
1
2
3QUOTE LOG - OPEN
4
5RFQ NAMECUSTOMER NAMEFOLLOW UP DATEAMOUNTQUOTE NUMBER(S)STATUS
6testcustomer test10/3/2023$5,373.001964654Open
7testcustomer test10/3/2023$7,878.001964654Open
8testcustomer test10/3/2023$54,613.001964654Open
9testcustomer test10/3/2023$9,823.001964654Open
10testcustomer test11/11/2023$53,751.001964654Open
11testcustomer test10/3/2023$2,456.001964654Open
12testcustomer test10/12/2023$7,878.001964654Open
13testcustomer test10/4/2023$783.001964654Open
14testcustomer test10/3/2023$7,867.001964654Open
15testcustomer test11/11/2023$562.001964654Open
16testcustomer test10/12/2023$8,956.001964654Open
17testcustomer test10/3/2023$16,546,547.001964654Open
18testcustomer test10/3/2023$3,858.001964654Open
19testcustomer test10/3/2023$1,852.001964654Open
20testcustomer test10/3/2023$15,236.001964654Open
21testcustomer test10/3/2023$965.001964654Open
22testcustomer test10/2/2023$1,234.001964654Open
23testcustomer test10/31/2023$6,541.001964654Open
24
25
Open
Cells with Data Validation
CellAllowCriteria
G6:G23ListOpen,Won,Lost


Quote Log - BM Test.xlsm
ABCDEFGHI
1
2
3QUOTE LOG - WON
4
5RFQ NAMECUSTOMER NAMEFOLLOW UP DATEAMOUNTQUOTE NUMBER(S)STATUSSALES ORDER
6
7
Won
Cells with Data Validation
CellAllowCriteria
G6ListOpen,Won,Lost



Quote Log - BM Test.xlsm
ABCDEFGHI
1
2
3QUOTE LOG - LOST
4
5RFQ NAMECUSTOMER NAMEFOLLOW UP DATEAMOUNTQUOTE NUMBER(S)STATUSREASON
6
7
Lost
Cells with Data Validation
CellAllowCriteria
G6ListOpen,Won,Lost
 
Upvote 0
Just needed a bit of tweaking, please try the following amended code on a copy of your workbook:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.CountLarge = 1 And Not Intersect(Range("G:G"), Target) Is Nothing Then
        On Error GoTo Escape
        Application.EnableEvents = False
        Dim s As String, i As Long
        s = Target.Value2
        If s <> "Open" Then
            Worksheets(s).ListObjects(s).ListRows.Add
            i = Worksheets(s).ListObjects(s).ListRows.Count
            Worksheets(s).ListObjects(s).DataBodyRange(i, 1).Resize(1, 6).Value = _
            Range("B" & Target.Row).Resize(1, 6).Value
            If Worksheets(s).ListObjects(s).DataBodyRange(1, 1) = "" Then _
            Worksheets(s).ListObjects(s).ListRows(1).Delete
            Me.ListObjects("Open").ListRows(Target.Row - 5).Delete
        End If
    End If
Continue:
    Application.EnableEvents = True
    Exit Sub
Escape:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume Continue
End Sub
 
Upvote 0
Solution
Perfect! That was exactly what I needed.
Thank you so much for your time and help!
 
Upvote 0
@kevin9999 What if I only wanted to take the contents of 1 cell from the open tab, let's say column F (Quote Number), and populate that cell data in column F of the Won or Lost tab. The difference is not taking the entire row, just the data in 1 cell?
 
Upvote 0
In that case, you would change this line of code:
VBA Code:
Worksheets(s).ListObjects(s).DataBodyRange(i, 1).Resize(1, 6).Value = _
Range("B" & Target.Row).Resize(1, 6).Value

To this:
VBA Code:
Worksheets(s).ListObjects(s).DataBodyRange(i, 5).Value = _
Range("F" & Target.Row).Value

Assuming of course that your table layout is exactly the same as it is in this thread. If your table(s) and sheet layouts are significantly different, then I suggest you start a new thread.
Hope this helps :)
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,975
Members
449,095
Latest member
Mr Hughes

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