VBA-Copy/Paste value to another column based on criteria

BigBeachBananas

Active Member
Joined
Jul 13, 2021
Messages
450
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi all,
I'm looking for a VBA that can search based on the criterion (male & female) in column A. I want to return all of the values in D to F and G based on gender. Please see attached for example. Thanks//
1638220681698.png
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
You mentioned you wanted a VBA solution. However this can be done via formula.
Place in cell F2 and copy and paste down.
Excel Formula:
=IFERROR(INDEX($D:$D,SMALL(IF($A:$A="M",ROW($A:$A),""),ROW(F1)),1),"")
Place in cell G2 and copy and paste down.
Excel Formula:
=IFERROR(INDEX($D:$D,SMALL(IF($A:$A="F",ROW($A:$A),""),ROW(G1)),1),"")

But if you prefer VBA then this may do the job.

VBA Code:
Option Explicit

Sub GetByGender()
Dim rng As Range
Dim rows As Long
Dim c As Variant
Dim i As Long
Dim mRow As Long
Dim fRow As Long

    'count used rows
    rows = ActiveSheet.UsedRange.rows.Count
    'set column A - Gender to perform loop search
Set rng = ActiveSheet.Range("A1", Cells(rows, 1))
    'set starting row #2 - skip headers
    mRow = 2
    fRow = 2
For Each c In rng
    If c = "M" Then
        'Male
        'column 6 = column F which is Males
        Cells(mRow, 6) = c.Offset(0, 3)
        mRow = mRow + 1
    ElseIf c = "F" Then
        'Female
        'column 7 = column G which is Females
        Cells(fRow, 7) = c.Offset(0, 3)
        fRow = fRow + 1
        
    Else
        'not designated - do nothing
    End If
Next c

MsgBox "Complete"

End Sub
 
Upvote 0
Solution
You mentioned you wanted a VBA solution. However this can be done via formula.
Place in cell F2 and copy and paste down.
Excel Formula:
=IFERROR(INDEX($D:$D,SMALL(IF($A:$A="M",ROW($A:$A),""),ROW(F1)),1),"")
Place in cell G2 and copy and paste down.
Excel Formula:
=IFERROR(INDEX($D:$D,SMALL(IF($A:$A="F",ROW($A:$A),""),ROW(G1)),1),"")

But if you prefer VBA then this may do the job.

VBA Code:
Option Explicit

Sub GetByGender()
Dim rng As Range
Dim rows As Long
Dim c As Variant
Dim i As Long
Dim mRow As Long
Dim fRow As Long

    'count used rows
    rows = ActiveSheet.UsedRange.rows.Count
    'set column A - Gender to perform loop search
Set rng = ActiveSheet.Range("A1", Cells(rows, 1))
    'set starting row #2 - skip headers
    mRow = 2
    fRow = 2
For Each c In rng
    If c = "M" Then
        'Male
        'column 6 = column F which is Males
        Cells(mRow, 6) = c.Offset(0, 3)
        mRow = mRow + 1
    ElseIf c = "F" Then
        'Female
        'column 7 = column G which is Females
        Cells(fRow, 7) = c.Offset(0, 3)
        fRow = fRow + 1
       
    Else
        'not designated - do nothing
    End If
Next c

MsgBox "Complete"

End Sub
Thanks for both options:)
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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