VBA - xlFilterCopy/CopyToRange - Paste Special to retain destination formatting?

BPW

New Member
Joined
Mar 7, 2013
Messages
31
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

I'm stuck and need a little guidance. I'm attempting to copy unique values within a column of one worksheet and paste them within another worksheet (same workbook) retaining the destination sheet's formatting. Below is the VBA that pastes over the formatting.

VBA Code:
Sub CreateUniqueList2()
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("Data Dump")
Set ws2 = Worksheets("rollups")

Dim lastrow As Long

lastrow = Cells(Rows.Count, "B").End(xlUp).Row


ws1.Activate

ws1.Range("B3:B" & lastrow).AdvancedFilter _
    Action:=xlFilterCopy, _
    CopyToRange:=ws2.Range("b4"), _
    Unique:=True
     
End Sub

Thanks in advance!
BPW
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
As you have 365, why not just use this formula
Excel Formula:
=UNIQUE(FILTER('Data Dump'!B3:B1000,'Data Dump'!B3:B1000<>""))
 
  • Like
Reactions: BPW
Upvote 0
That is a great solution, thank you! I did happen upon this awesome formula in my research but I wanted to try it this way to learn VBA. Any suggestions?
 
Upvote 0
As you have found using the advanced filter will copy everything rather than just values & I'm not sure that can be changed.
 
Upvote 0
Another option:
Before executing the filter, copy col B in ws2 and paste format to a helper column then after executing the filter, copy it back & paste format.
OR if you have another column that has the same format then you can just use that column.
 
  • Like
Reactions: BPW
Upvote 0
You could use the unique function in VBA like
VBA Code:
Sub CreateUniqueList2()
Dim ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("Data Dump")
Set ws2 = Worksheets("rollups")
Dim Ary As Variant
Dim lastrow As Long

lastrow = ws1.Cells(Rows.Count, "B").End(xlUp).Row
Ary = Application.Unique(ws1.Range("B3:B" & lastrow).Value)
ws2.Range("B4").Resize(UBound(Ary)).Value = Ary
End Sub
 
  • Like
Reactions: BPW
Upvote 0
Solution
Thanks Fluff and Akuini! I will try this out and let you know how I fare. :)
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Posting a follow-up. The script you provided Fluff worked phenomenally. Thank you both for your help and guidance!
 
Upvote 0

Forum statistics

Threads
1,214,786
Messages
6,121,553
Members
449,038
Latest member
Guest1337

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