VBA Move Row to Another Sheet Based on 2 Criteria

AMD00010

New Member
Joined
Jul 12, 2018
Messages
6
Hello --

Looking for help with the following situation.

I'd like to move an entire row from "Worksheet 1: Pending Items" to another Worksheet based on the value in Column 1 and the Status in Column 3. In my simplified example below (header row in bold), I'd like Row 3 from "Worksheet 1: Pending Items" to move to "Worksheet 2: Div 02" when the Status is marked close and because the Value in Column 1 is 02. This would repeat for all values in Column 1; i.e. Row 4 would move to "Worksheet 3: Div 03" when the Status in Column 3 is marked Closed. And so on.

Anyone have a solution I can try? Thanks in advance.

Worksheet 1: Pending Items
Column 1Column 2Column 3
Row 1 (Header)Div #Type.Status
Row 201Example 1Open
Row 302Example 2Closed
Row 403Example 3Closed

<tbody>
</tbody>

Worksheet 2: Div 02
Div #Type.Status
02Example 2Closed

<tbody>
</tbody>

Worksheet 3: Div 03
Div #Type.Status
03Example 3Closed

<tbody>
</tbody>
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Put this script in sheet named Pending.
This is an auto sheet event script
Your Workbook must be Macro enabled
To install this code:
Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window

Script will run when value "Closed" is entered into column C on sheet named Pending

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  7/14/2018  9:45:50 PM  EDT
If Not Intersect(Target, Range("C:C")) Is Nothing Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
Dim ans As Long
If Target.Value = "Closed" Then
Dim Lastrow As Long
ans = Target.Row
Lastrow = Sheets("Div " & Cells(ans, 2).Value).Cells(Rows.Count, "A").End(xlUp).Row + 1
Rows(ans).Copy Sheets("Div " & Cells(ans, 2).Value).Rows(Lastrow)
Rows(ans).Delete
End If
End If
End Sub
 
Upvote 0
ES8YuSAfmi9LuEPzgmcsyg0BZkekdSq-30Qzih1qW3ANcQ
ES8YuSAfmi9LuEPzgmcsyg0BZkekdSq-30Qzih1qW3ANcQ
onedrive.aspx
Thank you so much for your response. I inserted the script into the Pending worksheet and received an error that says "Run-time error '9': Subscript out of range." When I hit "De-bug" the 7th line of the script (starting "Lastrow = Sheets..") is highlighted. Any idea what's causing the issue?

I think it might be helpful if I supply some additional information.
- As background the spreadsheet is to manage requests for information related to a building under construction. All queries are organized by Division # - and Division #s typically range from 01-33. The Pending tab will list all queries submitted and pending a response. The additional tabbed worksheets will include all items closed (answered). So, we will have up to 33 additional tabs with closed queries listed as reference/for our record.
- Some queries will include a "Resubmit" response option (as opposed to Open or Closed). I found a script on the forum that moved the Resubmit items successfully to another worksheet called "Resubmit." I thought I could replicate the script, but got stuck when I realized the additional Criteria of the Division # would change the script I used. The script I successfully used for "Resubmit" is one you wrote back in 2016, found here: https://www.mrexcel.com/forum/excel-questions/956476-vba-move-row-another-sheet-based-criteria.html.
- I used a simplified version of my spreadsheet columns, but please let me know if I should post the example with the complete number of columns.

Any feedback/help very much appreciated. Please let me know if I can provide more details.

THANK YOU.
 
Upvote 0
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  7/16/2018  2:17:01 PM  EDT
If Not Intersect(Target, Range("C:C")) Is Nothing Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
Dim ans As Long
If Target.Value = "Closed" Then
Dim Lastrow As Long
ans = Target.Row
Lastrow = Sheets("Div " & Cells(ans, 1).Value).Cells(Rows.Count, "A").End(xlUp).Row + 1
Rows(ans).Copy Sheets("Div " & Cells(ans, 1).Value).Rows(Lastrow)
Rows(ans).Delete
End If
End If
End Sub
 
Upvote 0
Thank you. This works. However, I can't get the 2 scripts to go at once. Only one or the other. I'm getting the ambiguous name message. Advice? Sorry, I'm new and not sure how to resolve this on my own. THANK YOU.
 
Upvote 0
So I need to see the other script.
I see nothing in your original post about two scripts.
 
Upvote 0
My apologies.

Here's the other script:

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


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



Column C/Status has 3 total options: Open (no action occurs); Closed - Row moves to Div 1, or Div 2, etc. worksheet; Resubmit - Row moves to worksheet entitled Resubmit.
 
Upvote 0
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Modified  7/16/2018  4:26:07 PM  EDT
If Not Intersect(Target, Range("C:C")) Is Nothing Then
If Target.Cells.CountLarge > 1 Or IsEmpty(Target) Then Exit Sub
Dim ans As Long
Dim Lastrow As Long
Dim Lastrowa As Long
    If Target.Value = "Closed" Then
        ans = Target.Row
        Lastrow = Sheets("Div " & Cells(ans, 1).Value).Cells(Rows.Count, "A").End(xlUp).Row + 1
        Rows(ans).Copy Sheets("Div " & Cells(ans, 1).Value).Rows(Lastrow)
        Rows(ans).Delete
        Exit Sub
    End If

    If Target.Value = "Resubmit" Then
        ans = Target.Row
        Lastrowa = Sheets("Resubmit").Cells(Rows.Count, "C").End(xlUp).Row + 1
        Rows(ans).Copy Destination:=Sheets("Resubmit").Rows(Lastrowa)
    Rows(ans).Delete
    End If
End If
End Sub
 
Upvote 0
WOW. Thank you. This works in my test. I'll integrate it into the larger spreadsheet tomorrow. Many thanks for your time.:cool:
 
Upvote 0
Glad I was able to help you.
Come back here to Mr. Excel next time you need additional assistance.
WOW. Thank you. This works in my test. I'll integrate it into the larger spreadsheet tomorrow. Many thanks for your time.:cool:
 
Upvote 0

Forum statistics

Threads
1,215,446
Messages
6,124,896
Members
449,194
Latest member
JayEggleton

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