+ Reply to Thread
Results 1 to 4 of 4

Copy and paste: Cycle thru identical worksheets, remove duplicate values

  1. #1
    Registered User
    Join Date
    10-09-2023
    Location
    USA
    MS-Off Ver
    Version 2306
    Posts
    5

    Copy and paste: Cycle thru identical worksheets, remove duplicate values

    Hi all,
    I have taken my workbook and worksheets filter, copy, and paste macro as far as I can.

    The idea is to have a macro button on the "Summary" tab of my attached (and simplified) Excel file.

    The assigned macro will cycle through the subsequent worksheets to filter for the information I need, copy the filtered information, and then paste filtered information under its respective column on the "Summary" worksheet while also removing duplicate values (so that all values only appear once in each column on the "Summary" worksheet).

    The macro will then move to the next worksheet to complete the same task until the end of the worksheets.

    My VBA code for filtering, copying, and cycling work perfectly and as intended. But I am failing to 1) successfully paste the copied information on the "Summary" worksheet in the correct column before the macro moves to the next worksheet, and 2) as a result have also been unsuccessful in removing duplicate values after pasting.

    I've included some conditional formatting in my excel file to show the values that have duplicates, as well as a desired results worksheet.

    My VBA code is below. I don't know if what I am hoping to accomplish is possible, but right now things fall apart at the "Paste.RemoveDuplicates" line.

    Any help you can offer would be much appreciated.
    Thanks in advance.


    Sub CopyPasteRemoveDuplicates()

    ' Copy and paste but remove duplicates on destination sheet Macro

    Application.ScreenUpdating = False

    Dim r As Integer
    r = 1
    Dim sourceSheet As Worksheet
    Set sourceSheet = ActiveSheet

    Do

    On Error GoTo ErrorHandler

    Worksheets(ActiveSheet.Index + 1).Select

    On Error Resume Next

    Rows("1:1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1").AutoFilter Field:=1, Criteria1:="123456789", Operator:=xlFilterValues
    ActiveSheet.Range("$B$1").AutoFilter Field:=2, Criteria1:="<>0", Operator:=xlFilterValues

    Application.CutCopyMode = False

    ActiveSheet.Range("$B$2").Select
    ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("Summary").Cells(2, r).Paste.RemoveDuplicates Columns:=r, Header:=xlYes

    Loop

    ErrorHandler:
    Call sourceSheet.Activate

    Application.CutCopyMode = False

    End Sub
    Attached Files Attached Files

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Copy and paste: Cycle thru identical worksheets, remove duplicate values

    I don't think you can Paste and Remove Duplicates in one move. I think you'd need to Remove Duplicates, then Copy and Paste the de-duplicated range.

    I can't test the theory as iPads don't let you see, let alone run VBA code.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Copy and paste: Cycle thru identical worksheets, remove duplicate values

    Use of AdvancedFilter
    Please Login or Register  to view this content.
    Last edited by jindon; 10-09-2023 at 10:49 PM. Reason: typo

  4. #4
    Registered User
    Join Date
    10-09-2023
    Location
    USA
    MS-Off Ver
    Version 2306
    Posts
    5

    Re: Copy and paste: Cycle thru identical worksheets, remove duplicate values

    Thank you jindon for helping me brainstorm.
    I used your "Cells" code and included into mine. Now it works perfectly!
    To summarize the code: the macro goes to each worksheet, converts the data I want using Columns to Text so that the filters recognize the values as numbers, filters the columns by my criteria, then it copies and pastes the data I need into my main summary worksheet in sequence while removing duplicate values.
    Marking this thread as "solved"!

    Sub CopyPasteRemoveDuplicatesMerge()

    ' Copy and paste but remove duplicates on destination sheet Macro

    Application.ScreenUpdating = False

    Dim sourceSheet As Worksheet
    Set sourceSheet = ActiveSheet

    Dim i As Long

    Do

    i = i + 1

    On Error GoTo ErrorHandler

    Worksheets(ActiveSheet.Index + 1).Select

    ActiveSheet.Range("$B$2").Select
    ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
    Selection.TextToColumns Destination:=Range("$B$2"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
    :=Array(1, 1), TrailingMinusNumbers:=True

    On Error Resume Next

    ActiveSheet.Rows("1:1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1").AutoFilter Field:=1, Criteria1:="123456789", Operator:=xlFilterValues
    ActiveSheet.Range("$B$1").AutoFilter Field:=2, Criteria1:="<>0", Operator:=xlFilterValues

    ActiveSheet.Range("$B$2").Select
    ActiveSheet.Range(Selection, Selection.End(xlDown)).Select

    Selection.AdvancedFilter Action:=xlFilterCopy, _
    CopyToRange:=sourceSheet.Cells(2, i), Unique:=True

    Loop

    ErrorHandler:
    Call sourceSheet.Activate

    Application.CutCopyMode = False

    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 7
    Last Post: 08-21-2016, 11:16 AM
  2. [SOLVED] Trying to find duplicate, copy and paste certain unique values, and remove the duplicates
    By adamstarr12 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-24-2014, 04:21 AM
  3. [SOLVED] How to duplicate (hundreds) of identical worksheets?
    By agent46 in forum Excel General
    Replies: 10
    Last Post: 04-26-2013, 02:33 AM
  4. [SOLVED] Cycle Through A Series of Worksheets & Insert Rows, Then Special Paste Data
    By DDM64 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-26-2012, 10:45 PM
  5. [SOLVED] Cycle Through Specific Worksheets & Paste Data In Each Sheet
    By DDM64 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-18-2012, 11:59 PM
  6. Copy Paste Values all worksheets & creating new worksheets
    By ryan2600 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-05-2012, 11:46 AM
  7. Replies: 16
    Last Post: 09-06-2005, 05:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1