Copy Unique Values if Condition Met

andybason

Board Regular
Joined
Jan 7, 2012
Messages
217
Office Version
  1. 2016
Hello

I have a macro that copies unique values from one sheet to another.

It works ok but I need to add a condition so that it only copies the values if another cell on the same row as each value contains the word "Absent".

Is this possible? Any help would be appreciated.

Thank you


VBA Code:
Sub CopyIDs()

Dim lastrow As Long

lastrow = ThisWorkbook.Sheets("StudentIDs").Cells(Rows.Count, "A").End(xlUp).Row

    ThisWorkbook.Sheets("StudentIDs").Range("A2:A" & lastrow).AdvancedFilter _
    Action:=xlFilterCopy, _
    CopyToRange:=ThisWorkbook.Sheets("Summary").Range("A55"), _
    Unique:=True

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
It would help if you put an example of how your data is in each of your sheets.

But try adapting the following. Assuming that your headers are in row 1, that your data starts in row 2, that the "other cell" is in column "C", that the column "C" header is "Condition". Create two data in your sheet "StudentIDs" in two cells that you have available, in my example I am using cells Z1 and Z2, the header Z1 must be equal to the header C1 (following my assumption).

1669654655974.png


Try:

VBA Code:
Sub CopyIDs()
  Dim shS As Worksheet
  Dim lastrow As Long

  Set shS = ThisWorkbook.Sheets("StudentIDs")
  lastrow = shS.Cells(Rows.Count, "A").End(xlUp).Row
  shS.Range("A1:C" & lastrow).AdvancedFilter _
    Action:=xlFilterCopy, _
    CriteriaRange:=shS.Range("Z1:Z2"), _
    CopyToRange:=ThisWorkbook.Sheets("Summary").Range("A55"), _
    Unique:=True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,998
Messages
6,122,643
Members
449,093
Latest member
Ahmad123098

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