Moving rows based on more then one criteria

BOB101

New Member
Joined
Jul 29, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone,
I need some help. My VP tasked me with creating a new work sheet for our team. He wants when someone puts "Y" in column "J" the entire row will move to one of two possible tabs depending on what's in column "I". as we have multiple colored zones & the two people who work on them split them. The zones are Green, Yellow, & Blue would go to Hilary's tab & Pink, Red, Beach Blue, & Purple go to Paul. The zones are in column "I". I need a code that when someone puts a "Y" in column "J", that row will move to Hilary's or Paul's tabs depending on what we have in column "I".

Thank you
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You can do this by setting up a simple worksheet with the zones and worksheet names (I used the actual person's names as the sheet names for my example. You can change these to suit your needs or add new ones later). The code is looking for this worksheet named "Zone Lookup" with a range name of "ZoneLookup" on it as follows

Book8
AB
1ZoneSheet Name
2GreenHilary
3YellowHilary
4BlueJoe
5PinkPaul
6RedPaul
7Beach BluePaul
8PurplePaul
Zone Lookup


Then place this code in the Worksheet Change event of the "Master" worksheet
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Column = 10 And UCase(Target.Value) = "Y" Then
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        
        Dim lr As Long, zone As String
        Dim trgWsName As String
        Dim ws As Worksheet
        Dim mstrCellAddr As String
        
        mstrCellAddr = Target.Address
        zone = Target.Offset(0, -1).Value
        On Error Resume Next
        trgWsName = WorksheetFunction.VLookup(zone, Sheets("Zone Lookup").Range("ZoneLookup"), 2, 0)
        On Error GoTo 0
        
        If trgWsName <> "" Then
            If WsExists(trgWsName) Then
                With Sheets(trgWsName)
                    lr = .Cells(Rows.Count, 9).End(xlUp).Row + 1
                    Target.EntireRow.Copy .Range(lr & ":" & lr)
                    Target.EntireRow.Delete
                    Range(mstrCellAddr).Select
                End With
            Else
                MsgBox "The target worksheet could not be found.  No rows moved.", vbOKOnly
            End If
        Else
            MsgBox "The zone could not be found.  No rows moved.", vbOKOnly
        End If
        
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    End If
End Sub

And place this code into a module in the same workbook. this is a user defined function that will check that the target worksheet exists.
VBA Code:
Public Function WsExists(ws2find As String) As Boolean
    WsExists = False
    For Each Sheet In Worksheets
        If ws2find = Sheet.Name Then
            sheetExists = True
            Exit Function
        End If
    Next Sheet
End Function
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,638
Members
449,093
Latest member
Ahmad123098

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