Copy rows from sheet1 to sheet2 & sheet3 based on cell value

Hi Experts,

I am a newbie in vba and I have a Expense Monitoring (master sheet) worksheet. If the value in column F = "admin" it copies the entire row to Sheet2(Admin Expense) but if the value in column F = "direct" it also copies the entire row to sheet3(Direct Expense).
 
It keeps overwriting the data in the destination sheet, the sheet2(admin expense) and sheet3(direct expense).
It seems that the code to paste in the next empty row in the destination sheet is not working.

I used this code:

Private Sub Worksheet_Change(ByVal Target As Range)
'stop anything re-triggering this event macro
Application.EnableEvents = False

On Error GoTo ErrHnd

'test if changed cell is in column F (col. 6) and it contains ADMIN (or admin)
If Target.Column = 6 And UCase(Target.Text) = "ADMIN" Then
    Dim rngCell As Range
    Dim rngDest As Range
    Dim strRowAddr As String
   
    'save target row address
    strRowAddr = Target.Address
   
    'find next row in destination worksheet
    Set rngDest = Worksheets("ADMIN_EXPENSES"). _
               Range("A" & CStr(Application.Rows.Count)).End(xlUp).Offset(1, 0)

    'copy the source row & paste to destination
    Target.EntireRow.Copy Destination:=rngDest
    'remove the copy range marquee
    Application.CutCopyMode = False
   
End If
'test if changed cell is in column F (col. 6) and it contains DIRECT (or direct)
If Target.Column = 6 And UCase(Target.Text) = "DIRECT" Then
   
    'save target row address
    strRowAddr = Target.Address
   
    'find next row in destination worksheet
    Set rngDest = Worksheets("DIRECT_COST_EXPENSES"). _
               Range("A" & CStr(Application.Rows.Count)).End(xlUp).Offset(1, 0)

    'copy the source row & paste to destination
    Target.EntireRow.Copy Destination:=rngDest
    'remove the copy range marquee
    Application.CutCopyMode = False
   
End If
Application.EnableEvents = True
Exit Sub

'error handler
ErrHnd:
Err.Clear
Application.EnableEvents = True
End Sub
atoydequitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
Use this code...

Private Sub Worksheet_Change(ByVal Target As Range)
'stop anything re-triggering this event macro
    Application.EnableEvents = False

    Dim lr As Long, ws As Worksheet
    On Error GoTo ErrHnd

    'test if changed cell is in column F (col. 6) and it contains ADMIN (or admin)
    If Target.Column = 6 And UCase(Target.Text) = "ADMIN" Then

        Set ws = Sheets("ADMIN_EXPENSES")

        lr = ws.Cells(Cells.Rows.Count, 6).End(xlUp).Row + 1

        Target.EntireRow.Copy ws.Range("A" & lr)
        Application.CutCopyMode = False

    End If
    'test if changed cell is in column F (col. 6) and it contains DIRECT (or direct)
    If Target.Column = 6 And UCase(Target.Text) = "DIRECT" Then
        Set ws = Sheets("DIRECT_COST_EXPENSES")

        lr = ws.Cells(Cells.Rows.Count, 6).End(xlUp).Row + 1

        Target.EntireRow.Copy ws.Range("A" & lr)
        Application.CutCopyMode = False
    End If
    Application.EnableEvents = True
    Exit Sub

    'error handler
ErrHnd:
    Err.Clear
    Application.EnableEvents = True
End Sub

Open in new window


Saurabh...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rgonzo1971Commented:
Hi,

pls try

Private Sub Worksheet_Change(ByVal Target As Range)
 'stop anything re-triggering this event macro
 Application.EnableEvents = False

 On Error GoTo ErrHnd

 'test if changed cell is in column F (col. 6) and it contains ADMIN (or admin)
 If Target.Column = 6 And UCase(Target.Text) = "ADMIN" Then
     Dim rngCell As Range
     Dim rngDest As Range
     Dim strRowAddr As String
     
     'save target row address
     strRowAddr = Target.Address
     
     'find next row in destination worksheet
     Set rngDest = Worksheets("ADMIN_EXPENSES"). _
                Range("F" & CStr(Application.Rows.Count)).End(xlUp).Offset(1, 0)

     'copy the source row & paste to destination
     Target.EntireRow.Copy Destination:=rngDest
     'remove the copy range marquee
     Application.CutCopyMode = False
     
 End If
 'test if changed cell is in column F (col. 6) and it contains DIRECT (or direct)
 If Target.Column = 6 And UCase(Target.Text) = "DIRECT" Then
     
     'save target row address
     strRowAddr = Target.Address
     
     'find next row in destination worksheet
     Set rngDest = Worksheets("DIRECT_COST_EXPENSES"). _
                Range("F" & CStr(Application.Rows.Count)).End(xlUp).Offset(1, 0)

     'copy the source row & paste to destination
     Target.EntireRow.Copy Destination:=rngDest
     'remove the copy range marquee
     Application.CutCopyMode = False
    
 End If
 Application.EnableEvents = True
 Exit Sub

 'error handler
 ErrHnd:
 Err.Clear
 Application.EnableEvents = True
 End Sub 

Open in new window

Regards
0
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.