Copy range based on criteria in column - macro

doriannjeshi

Active Member
Joined
Apr 5, 2015
Messages
301
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
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Maybe something like....

VBA Code:
Sub doriannjeshi()
    Dim actSht As Worksheet
    
    Application.ScreenUpdating = False
    Set actSht = Sheets("Sheet1")

    Worksheets.Add(after:=Sheets(Sheets.Count)).Name = "Male"
    Worksheets.Add(after:=Sheets(Sheets.Count)).Name = "Female"

    With actSht.Range("A1:F" & actSht.Range("A" & Rows.Count).End(xlUp).Row)
    
        .AutoFilter 5, "Male"
        
        On Error Resume Next
        With .SpecialCells(xlCellTypeVisible)
            .Copy Sheets("Male").Range("A1")
        End With
        On Error GoTo 0
        
        actSht.ShowAllData
        
    End With
    
    With actSht.Range("A1:F" & actSht.Range("A" & Rows.Count).End(xlUp).Row)
    
        .AutoFilter 5, "Female"
        
        On Error Resume Next
        With .SpecialCells(xlCellTypeVisible)
            .Copy Sheets("Female").Range("A1")
        End With
        On Error GoTo 0
        
       .AutoFilter
       actSht.Activate
    End With
    
    Application.ScreenUpdating = True

End Sub
 
Last edited:
Upvote 0
A slightly different approach, assuming sheets 'Male' and 'Female' do not already exist in the workbook.

VBA Code:
Sub MaleFemale()
  Application.ScreenUpdating = False
  With Sheets("Sheet1") '<- Change sheet name if required
    .Copy After:=Sheets(.Index)
    With ActiveSheet
      .Name = "Female"
      With .UsedRange
        .AutoFilter Field:=5, Criteria1:="<>Female"
        .Offset(1).EntireRow.Delete
        .AutoFilter
      End With
    End With
    .Copy After:=Sheets(.Index)
    With ActiveSheet
      .Name = "Male"
      With .UsedRange
        .AutoFilter Field:=5, Criteria1:="<>Male"
        .Offset(1).EntireRow.Delete
        .AutoFilter
      End With
    End With
    .Activate
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
.. or a more compact form

VBA Code:
Sub MaleFemale_v2()
  Dim sht As Variant
  
  Application.ScreenUpdating = False
  With Sheets("Sheet1") '<- Change sheet name if required
    For Each sht In Split("Female|Male", "|")
      .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
    .Activate
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
.. or a more compact form

VBA Code:
Sub MaleFemale_v2()
  Dim sht As Variant
 
  Application.ScreenUpdating = False
  With Sheets("Sheet1") '<- Change sheet name if required
    For Each sht In Split("Female|Male", "|")
      .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
    .Activate
  End With
  Application.ScreenUpdating = True
End Sub
Really nice, columns also autofit
Thank you!
 
Upvote 0
HI,
Is it possible to have male and female split from the sheet together in new sheet to be named "married", and bigender in another sheet named "bigender" ?
 
Upvote 0
  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.
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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