siunnicuaid
New Member
- Joined
- Mar 5, 2021
- Messages
- 3
- Office Version
- 365
- Platform
- Windows
I'm trying to create a macro which will copy the unique values in Columns F and X from a worksheet (Data) to a new workbook where column C = Yes.
Below is copying the entire Data sheet into the new workbook - how do I set the range (rData) to only columns F and X (and only copy a unique value).
Below is copying the entire Data sheet into the new workbook - how do I set the range (rData) to only columns F and X (and only copy a unique value).
VBA Code:
Sub CreateDistribution()
Dim ws As Worksheet
Dim wsNew As Workbook
Dim rData As Range
Dim sfilename As String
Dim dDate As String
Set ws = ThisWorkbook.Sheets("Data")
With ws
Set rData = .Range(.Cells(1, 1), .Cells(.Rows.Count, 24).End(xlUp))
.Columns(.Columns.Count).Clear
.Range(.Cells(2, 6), .Cells(.Rows.Count, 6).End(xlUp)).AdvancedFilter Action:=xlFilterCopy, CopytoRange:=.Cells(1, .Columns.Count), Unique:=True
Set wsNew = Workbooks.Add
dDate = Format(Now, "yyyymmd")
sfilename = "ManagerDistribution" & " " & dDate & ".xlsx"
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & sfilename
Application.DisplayAlerts = False
ws.Activate
rData.AutoFilter Field:=3, Criteria1:="Yes"
rData.Copy
Workbooks(sfilename).Activate
ActiveSheet.Paste
ActiveWorkbook.Close SaveChanges:=True
Application.DisplayAlerts = True
End With
ws.Columns(Columns.Count).ClearContents
rData.AutoFilter
End Sub