Copy range based on criteria in column - macro

doriannjeshi

Active Member
Joined
Apr 5, 2015
Messages
338
Office Version
  1. 365
Platform
  1. Windows
Hi,
how to copy the entire data only for "male" to a new sheet named "male" , also for female. Using a macro


Book2
ABCDEF
1idfirst_namelast_nameemailgenderip_address
21RikkiPererpere0@alexa.comMale61.164.210.42
32VickAmbrozikvambrozik1@google.itMale102.222.176.131
43KaliSheltonkshelton2@slate.comBigender209.89.60.163
54LodovicoShearslshears3@bandcamp.comMale175.100.178.34
65RhodaGavaranrgavaran4@alexa.comFemale131.26.160.86
76GerriNealegneale5@independent.co.ukMale76.95.229.131
87TodEmmitttemmitt6@dot.govMale249.234.72.77
98KillyLouysklouys7@networksolutions.comMale226.205.57.155
109JohnnaManlowjmanlow8@freewebs.comFemale66.160.3.172
1110ShandeighPossellspossell9@blogspot.comFemale54.79.95.159
1211ThelmaCorballistcorballisa@mediafire.comFemale209.246.69.109
1312ChristabellaMcCowancmccowanb@skyrock.comFemale254.29.4.81
1413TristanStedetstedec@weibo.comMale58.238.118.14
1514KlaraStorreskstorresd@parallels.comFemale103.61.128.129
1615BendixCavnorbcavnore@addthis.comPolygender115.197.228.198
1716PammiWhilespwhilesf@archive.orgFemale235.85.253.158
1817SenaFothergillsfothergillg@indiatimes.comFemale121.116.167.159
1918OfeliaSargisonosargisonh@uiuc.eduFemale30.29.132.119
2019OberonEndoendi@dedecms.comMale19.176.34.160
2120CaresaKybbyeckybbyej@a8.netAgender157.221.2.249
2221SaundraYeldingsyeldingk@reddit.comMale240.178.148.42
2322DaisyParnelldparnelll@home.plFemale5.136.226.91
2423PrueCorranpcorranm@istockphoto.comFemale16.235.134.99
2524GamalielMoyseygmoyseyn@t-online.deMale56.38.94.245
2625PebrookQuinionpquiniono@naver.comMale88.85.159.192
2726DouglassRossidrossip@yahoo.co.jpMale20.52.3.13
2827KayneMertonkmertonq@fotki.comMale228.35.108.191
2928OonaBiddlestoneobiddlestoner@trellian.comFemale25.80.120.56
3029TheadoraBlacktintblacktins@jimdo.comFemale7.174.1.190
3130PheliaRykertprykertt@ning.comFemale128.142.83.253
Sheet1


result

Book2
ABCDEF
1idfirst_namelast_nameemailgenderip_address
21RikkiPererpere0@alexa.comMale61.164.210.42
32VickAmbrozikvambrozik1@google.itMale102.222.176.131
44LodovicoShearslshears3@bandcamp.comMale175.100.178.34
56GerriNealegneale5@independent.co.ukMale76.95.229.131
67TodEmmitttemmitt6@dot.govMale249.234.72.77
78KillyLouysklouys7@networksolutions.comMale226.205.57.155
813TristanStedetstedec@weibo.comMale58.238.118.14
919OberonEndoendi@dedecms.comMale19.176.34.160
1021SaundraYeldingsyeldingk@reddit.comMale240.178.148.42
1124GamalielMoyseygmoyseyn@t-online.deMale56.38.94.245
1225PebrookQuinionpquiniono@naver.comMale88.85.159.192
1326DouglassRossidrossip@yahoo.co.jpMale20.52.3.13
1427KayneMertonkmertonq@fotki.comMale228.35.108.191
male
 
  1. How do we know who is married? Surely not every male and every female is married?

  2. Is this request in addition to the previous one (that is, do we still generate 'male' and 'female' sheets? Or is is a completely separate question with only two sheets generated
BTW, are those real names and real email addresses? If so I will remove them and replace with fakes.
Hi Peter,
this is bogus data , not real

Yes is the same request but this time content of "male","female" needs to be copied together to a new sheet "married" and what else is specified i.e Bigender split into their respective sheets
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
this is bogus data , not real
Great, thanks. (y)

and what else is specified i.e Bigender split into their respective sheets
Not entirely clear on that.
  • Bigender, Polygender, Agender all to one sheet, or
  • Bigender to one sheet, Polygender to another sheet, Agender to another sheet.
Do I assume that none of the 'Married' or other gender sheets already exist?
 
Upvote 0
Great, thanks. (y)


Not entirely clear on that.
  • Bigender, Polygender, Agender all to one sheet, or
  • Bigender to one sheet, Polygender to another sheet, Agender to another sheet.
Do I assume that none of the 'Married' or other gender sheets already exist?
1. No seperate sheets, named after them. But only copy the content to another sheet of what I specifiy i.e Bigender ok , Polygender not copied at all if not specified
2.Yes seperately , but only those specified

the sheets do not exist but need to be created

copy - male, female both to a new sheet to be named Married
copy - what else is specified to a each a sheet
 
Upvote 0
OK, try this

VBA Code:
Sub MaleFemale_v3()
  Dim sht As Variant
  
  Application.ScreenUpdating = False
  With Sheets("Sheet1")
    For Each sht In Split("Agender|Polygender|Bigender", "|")
      .Copy After:=Sheets(.Index)
      With ActiveSheet
        .Name = sht
        With .UsedRange
          .AutoFilter Field:=5, Criteria1:="<>" & sht
          .Offset(1).EntireRow.Delete
          .AutoFilter
        End With
      End With
    Next sht
    .Copy After:=Sheets(.Index)
    With ActiveSheet
      .Name = "Married"
      With .UsedRange
        .AutoFilter Field:=5, Criteria1:="<>Male", Operator:=xlAnd, Criteria2:="<>Female"
        .Offset(1).EntireRow.Delete
        .AutoFilter
      End With
    End With
    .Activate
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
OK, try this

VBA Code:
Sub MaleFemale_v3()
  Dim sht As Variant
 
  Application.ScreenUpdating = False
  With Sheets("Sheet1")
    For Each sht In Split("Agender|Polygender|Bigender", "|")
      .Copy After:=Sheets(.Index)
      With ActiveSheet
        .Name = sht
        With .UsedRange
          .AutoFilter Field:=5, Criteria1:="<>" & sht
          .Offset(1).EntireRow.Delete
          .AutoFilter
        End With
      End With
    Next sht
    .Copy After:=Sheets(.Index)
    With ActiveSheet
      .Name = "Married"
      With .UsedRange
        .AutoFilter Field:=5, Criteria1:="<>Male", Operator:=xlAnd, Criteria2:="<>Female"
        .Offset(1).EntireRow.Delete
        .AutoFilter
      End With
    End With
    .Activate
  End With
  Application.ScreenUpdating = True
End Sub
very nice ,
How do I add to the married sheet ?
.AutoFilter Field:=5, Criteria1:="<>Male", Operator:=xlAnd, Criteria2:="<>Female", Operator:=xlAnd, Criteria3:="<>Polygender"
 
Upvote 0
How do I add to the married sheet ?
:confused: The code does that
Rich (BB code):
Sub MaleFemale_v3()
  Dim sht As Variant
 
  Application.ScreenUpdating = False
  With Sheets("Sheet1")
    For Each sht In Split("Agender|Polygender|Bigender", "|")
      .Copy After:=Sheets(.Index)
      With ActiveSheet
        .Name = sht
        With .UsedRange
          .AutoFilter Field:=5, Criteria1:="<>" & sht
          .Offset(1).EntireRow.Delete
          .AutoFilter
        End With
      End With
    Next sht
    .Copy After:=Sheets(.Index)
    With ActiveSheet
      .Name = "Married"
      With .UsedRange
        .AutoFilter Field:=5, Criteria1:="<>Male", Operator:=xlAnd, Criteria2:="<>Female"
        .Offset(1).EntireRow.Delete
        .AutoFilter
      End With
    End With
    .Activate
  End With
  Application.ScreenUpdating = True
End Sub

Here are my tabs after starting with just 'Sheet1' and running the above code.

1730931198545.png
 
Last edited:
Upvote 0
@Peter_SSs the OP is trying to add a third criteria
Thanks Mark. Careless on my part - I read this without the "to" 😎
How do I add to the married sheet ?

@doriannjeshi
Sorry about misreading post 15. See if this is what you want then.
  • Each gender value that you want to go to an individual sheet, list between "|" characters in the "IndividualSheets" line near the top of the code below.
  • Everything else will get combined into a single "Married" sheet.
VBA Code:
Sub Gender_Split()
  Dim sht As Variant, IndividualSheets As Variant
  
  IndividualSheets = Split("Agender|Bigender", "|") '<- Specify any idividual sheets here
  
  Application.ScreenUpdating = False
  With Sheets("Sheet1")
  
    'This section creates the "individual" sheets
    For Each sht In IndividualSheets
      .Copy After:=Sheets(.Index)
      With ActiveSheet
        .Name = sht
        With .UsedRange
          .AutoFilter Field:=5, Criteria1:="<>" & sht
          .Offset(1).EntireRow.Delete
          .AutoFilter
        End With
      End With
    Next sht
    
    'This section creates a single sheet for all those EXCEPT the individual ones listed above.
    .Copy After:=Sheets(.Index)
    With ActiveSheet
      .Name = "Married"
      With .UsedRange
        .AutoFilter Field:=5, Criteria1:=IndividualSheets, Operator:=xlFilterValues
        .Offset(1).EntireRow.Delete
        .AutoFilter
      End With
    End With
    
    .Activate
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks Mark. Careless on my part - I read this without the "to" 😎


@doriannjeshi
Sorry about misreading post 15. See if this is what you want then.
  • Each gender value that you want to go to an individual sheet, list between "|" characters in the "IndividualSheets" line near the top of the code below.
  • Everything else will get combined into a single "Married" sheet.
VBA Code:
Sub Gender_Split()
  Dim sht As Variant, IndividualSheets As Variant
 
  IndividualSheets = Split("Agender|Bigender", "|") '<- Specify any idividual sheets here
 
  Application.ScreenUpdating = False
  With Sheets("Sheet1")
 
    'This section creates the "individual" sheets
    For Each sht In IndividualSheets
      .Copy After:=Sheets(.Index)
      With ActiveSheet
        .Name = sht
        With .UsedRange
          .AutoFilter Field:=5, Criteria1:="<>" & sht
          .Offset(1).EntireRow.Delete
          .AutoFilter
        End With
      End With
    Next sht
  
    'This section creates a single sheet for all those EXCEPT the individual ones listed above.
    .Copy After:=Sheets(.Index)
    With ActiveSheet
      .Name = "Married"
      With .UsedRange
        .AutoFilter Field:=5, Criteria1:=IndividualSheets, Operator:=xlFilterValues
        .Offset(1).EntireRow.Delete
        .AutoFilter
      End With
    End With
  
    .Activate
  End With
  Application.ScreenUpdating = True
End Sub
Thank you Peter and Mark,
What about the ones that I don't want to appear in any sheet ?
 
Last edited:
Upvote 0
Thank you Peter and Mark,
What about the ones that I don't want to appear in any sheet ?
This has not been mentioned before. Just as Married was not mentioned at the start.
You need to specify your requirements (all of them) at the beginning rather than getting an answer and then changing the requirement continually. :(
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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