VBA to copy non-sequential columns to new workbook, only unique values

siunnicuaid

New Member
Joined
Mar 5, 2021
Messages
3
Office Version
  1. 365
Platform
  1. 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).

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
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi & welcome to MrExcel.
Can cols F & X contain the same values?
Also do you want the results in one column or two?
 
Upvote 0
Hi & welcome to MrExcel.
Can cols F & X contain the same values?
Also do you want the results in one column or two?
No, F and X are different. F contains a name and X an email address, so I'm looking for the unique values across two columns - as though I de-duplicated them.

I want the results in 2 columns on my new workbook

Thanks for your help - I'm not great at VBA and totally lost!
 
Upvote 0
So if you had the same name in col F with different emails in X they should be kept?
 
Upvote 0
Sorry for the late reply - Yes - for each unique value for F, X can differ, so I'd need the combinations pasted over to the new sheet.

The data could look like

Column FColumn X
Bobbob1@gmail
Marymary@gmail
Susysusy@gmail
Bobbob2@gmail
Marymary@gmail
 
Upvote 0
Ok, how about
VBA Code:
Sub siunnicuaid()
   Dim wsData As Worksheet
   Dim Wbk As Workbook
   Dim UsdRws As Long
   
   Set wsData = ThisWorkbook.Sheets("Data")
   Set Wbk = Workbooks.Add
   
   Wbk.SaveAs "ManagerDistribution" & Format(Date, "yyyymmd") & ".xlsx", 51
   
   With wsData
      UsdRws = .Range("F" & Rows.Count).End(xlUp).Row
      .Range("F1:F" & UsdRws & ",X1:X" & UsdRws).Copy Wbk.Sheets(1).Range("A1")
   End With
   With Wbk.Sheets(1)
      .Range("A1:B" & UsdRws).RemoveDuplicates Array(1, 2), xlYes
   End With
   Wbk.Close True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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