'Object required' code error

Trick

New Member
Joined
Aug 6, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Trying to do a Macro (I think that's the correct terminology) but it comes up with 'Target.Column <Object Required>
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 2 And Target.Cells.Count = 1 Then
        If LCase(Target.Value) = "male" Then
            With Target.EntireRow
                .Copy Sheets("Males").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                .Delete
            End With
        If LCase(Target.Value) = "female" Then
            With Target.EntireRow
                .Copy Sheets("Females").Range("A" & Rows.Count).End(x1Up).Offset(1, 0)
                .Delete
            End With
        End If
    End If
End Sub
 
Last edited by a moderator:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
You had a few other errors, try...

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 2 And Target.Cells.Count = 1 Then
        If LCase(Target.Value) = "male" Then
            With Target.EntireRow
                .Copy Sheets("Males").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                .Delete
            End With
        ElseIf LCase(Target.Value) = "female" Then
            With Target.EntireRow
                .Copy Sheets("Females").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                .Delete
            End With
        End If
    End If
End Sub
 
Upvote 0
Hi, welcome to forum.
Looking at your code I suspect the error occurs because you have deleted the Target range in previous If test - maybe

Try this update to the code & see if does what you want

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim m As Variant
    With Target
        If .Column = 2 And .Cells.Count = 1 Then
            m = Application.Match(.Value, Array("Male", "Female"), 0)
            If Not IsError(m) Then
                .EntireRow.Copy Sheets(Choose(Val(m), "Males", "Females")).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                .EntireRow.Delete
            End If
        End If
    End With
End Sub

Dave
 
Upvote 0
Omg thank you heaps!!!!
ElseIf =D
Happy it helped and welcome to the board from me as well.

Just to point out the other error that you had in your original code was you had and End(x1Up) which should be End(xlUp)... a lowercase L not a 1 (I also thought that you had the wrong number of End Ifs for the number of If's but that doesn't appear to be the case now).

I notice that a Moderator has amended your first post to put code tags around the code to make it easier to read and copy (and indented it).
It might be worth considering using code tags in any future posts (some ways to do this are in my signature block below) ;)
 
Upvote 0
I'd also suggest you read the forum rules on cross-posting, please. ;)
 
Upvote 0

Forum statistics

Threads
1,214,570
Messages
6,120,297
Members
448,954
Latest member
EmmeEnne1979

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