+ Reply to Thread
Results 1 to 27 of 27

Combine data from multiple sheets and remove duplicates if any

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-11-2011
    Location
    Nepal
    MS-Off Ver
    MS Excel 2024
    Posts
    1,390

    Combine data from multiple sheets and remove duplicates if any

    I have data in multiple worksheets in specific columns.I want all of them to be in a two columns with no duplicates.Sample data and expected results are shown in Worksheet'Expected Result' in column A and B.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Combine data from multiple sheets and remove duplicates if any

    How about (Run "jec"):

    Sub jec()
     Dim sh, ar, k, i As Long
     With CreateObject("scripting.dictionary")
       For Each sh In ThisWorkbook.Sheets
         If sh.Index > 1 Then
           ar = sh.Range("C6", sh.Range("C" & Rows.Count).End(xlUp))
           For i = 1 To UBound(ar)
              k = getNum(ar(i, 1))
             .Item(k) = Array(k, ar(i, 1))
           Next
         End If
       Next
      Sheets("Expected Result").Range("A2").Resize(.Count, 2) = Application.Index(.items, 0, 0)
     End With
    End Sub
    
    
    Function getNum(cell As Variant) As Variant
     With CreateObject("vbscript.regexp")
       .Global = True
       .Pattern = "\d{9}"
        If .test(cell) Then
          getNum = .Execute(cell)(0)
        Else
          getNum = cell
        End If
     End With
    End Function

  3. #3
    Forum Contributor
    Join Date
    03-11-2011
    Location
    Nepal
    MS-Off Ver
    MS Excel 2024
    Posts
    1,390

    Re: Combine data from multiple sheets and remove duplicates if any

    Quote Originally Posted by JEC. View Post
    How about (Run "jec"):

    Sub jec()
     Dim sh, ar, k, i As Long
     With CreateObject("scripting.dictionary")
       For Each sh In ThisWorkbook.Sheets
         If sh.Index > 1 Then
           ar = sh.Range("C6", sh.Range("C" & Rows.Count).End(xlUp))
           For i = 1 To UBound(ar)
              k = getNum(ar(i, 1))
             .Item(k) = Array(k, ar(i, 1))
           Next
         End If
       Next
      Sheets("Expected Result").Range("A2").Resize(.Count, 2) = Application.Index(.items, 0, 0)
     End With
    End Sub
    
    
    Function getNum(cell As Variant) As Variant
     With CreateObject("vbscript.regexp")
       .Global = True
       .Pattern = "\d{9}"
        If .test(cell) Then
          getNum = .Execute(cell)(0)
        Else
          getNum = cell
        End If
     End With
    End Function
    Kindly add sheet name in code (As I will be combining specific sheet name and not all) with sorted alphabetically.

  4. #4
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Combine data from multiple sheets and remove duplicates if any

    Sub integratie_Oeldere_revisted_vs3()
    
    'I got a lot of help from AB33, to get this code working; thanks for that AB33.
    
    Dim wsTest As Worksheet
    
    'check if sheet "Consolidated" already exist
    
    Const strSheetName As String = "Consolidated"
     
    Set wsTest = Nothing
    On Error Resume Next
    Set wsTest = ActiveWorkbook.Worksheets(strSheetName)
    On Error GoTo 0
     
    If wsTest Is Nothing Then
        Worksheets.Add.Name = strSheetName
    End If
    
    With Sheets("Consolidated")
        .UsedRange.ClearContents
        .Range("A1:D1").Value = Array("sheet", "Code", "Delete", "Name")
        For Each Sh In Sheets
            With Sh
                If .Name <> "Consolidated" And .Name <> "Summary" And .Name <> "Expected Result" And .Name <> "PivotTable" Then
                     lr = .Cells(.Rows.Count, 3).End(xlUp).Row
                    If lr >= 2 Then
                       Rng = .Cells.Find("*", , , , xlByRows, xlPrevious).Row - 1
                       NR = Sheets("Consolidated").Cells.Find("*", , , , xlByRows, xlPrevious).Row + 1
                       If Rng > 0 Then
                        Sheets("Consolidated").Cells(NR, 1).Resize(Rng) = .Name
                        Sheets("Consolidated").Cells(NR, 2).Resize(Rng, 4) = .Range("A2").Resize(Rng, 4).Value
                      End If
                   End If
                End If
            End With
        Next
        On Error Resume Next
        .Range("D2:D" & .Rows.Count).SpecialCells(4).EntireRow.Delete
        .Range("C:C").EntireColumn.Delete
        .Range("D2:D" & .Rows.Count).RowHeight = 15
        
    
       For Each cl In .Range("C2:C" & .Rows.Count)
        If cl = "List" Then
        cl.Rows.EntireRow.Delete
        End If
        Next
     
    With Range("C1", Range("C" & Rows.Count).End(xlUp))
         .Remov_eDuplicates 1, Header:=xlYes
    End With
     
    With Range("B2:B" & Range("A" & Rows.Count).End(xlUp).Row)
         .FormulaR1C1 = "=MAX(IFERROR(MID(RC[1],ROW(R1:R100),COLUMN(C1:C26))*1,""""))"
    
    'it adds an @ before the formula and that result in "value"
    'I remove the @ manualy from the formula and copied the date down.
            
    End With
     
    ' With Columns("B")
     
    '.ActiveCell.Replace What:="@", Replacement:="", LookAt:=xlPart, _
    '        SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
    '        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
       
     
    ' End With
     
        .Columns("A:Z").EntireColumn.AutoFit
    End With
    End Sub
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  5. #5
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Combine data from multiple sheets and remove duplicates if any

    Hi. My ver

    Sub Unify()
    Dim ws As Integer, C As Range, D As Range
    Application.ScreenUpdating = False
    Range("A1").CurrentRegion.Offset(1).Delete xlShiftUp
    Columns("C:G").Delete xlShiftUp
    
    For ws = 1 + ActiveSheet.Index To Worksheets.Count
      With Worksheets(ws)
        .Range("C6", .Cells(Rows.Count, "C").End(xlUp)).Copy Cells(Rows.Count, "B").End(xlUp).Offset(1)
      End With
    Next
    
    With Range("B2", Range("B1").End(xlDown))
      .Replace What:=vbLf, Replacement:="", LookAt:=xlPart
      .TextToColumns Destination:=Range("C2"), DataType:=xlDelimited, Other:=True, OtherChar:="("
      Columns("C").Delete
      For Each C In .Offset(, 1)
        If C.Row > 1 Then
          Set D = C
          Do Until D = Empty
            If IsNumeric(Left(D, 9)) Then
              Cells(D.Row, "A") = 0 + Left(D, 9)
              Exit Do
            Else
              Set D = D.Offset(, 1)
            End If
          Loop
        End If
      Next
    End With
    
    Columns("C:G").Delete xlShiftUp
    With Range("A1").CurrentRegion
      .Sort Range("A1"), 1, Key2:=Range("B1"), Order2:=1, Header:=xlYes
      Range("G2") = "=CountIf(" & .Address & ", A2)>1"
      .AdvancedFilter 2, Range("G1:G2"), Range("D1"), False
    End With
    Range("G2").Clear: Columns("A:E").AutoFit: Columns("A:E").VerticalAlignment = xlCenter
    End Sub
    Attached Files Attached Files

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2507 Win 11
    Posts
    24,918

    Re: Combine data from multiple sheets and remove duplicates if any

    An alternative to VBA is Power Query

    let
        Source = Table.Combine({A, L, M, P}),
        #"Removed Columns" = Table.RemoveColumns(Source,{"Column1"}),
        #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each ([Column2] <> "" and [Column2] <> "List")),
        #"Removed Duplicates" = Table.Distinct(#"Filtered Rows")
    in
        #"Removed Duplicates"
    Power Query is a free AddIn for Excel 2010 and 2013, and is built-in functionality from Excel 2016 onwards (where it is referred to as "Get & Transform Data").

    It is a powerful yet simple way of getting, changing and using data from a broad variety of sources, creating steps which may be easily repeated and refreshed. I strongly recommend learning how to use Power Query - it's among the most powerful functionalities of Excel.

    - Follow this link to learn how to install Power Query in Excel 2010 / 2013.

    - Follow this link for an introduction to Power Query functionality.

    - Follow this link for a video which demonstrates how to use Power Query code provided.
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  7. #7
    Forum Contributor
    Join Date
    03-11-2011
    Location
    Nepal
    MS-Off Ver
    MS Excel 2024
    Posts
    1,390

    Re: Combine data from multiple sheets and remove duplicates if any

    Post# 3 & 4 doesn't seems to work as per expected results in Post#1.
    Last edited by paradise2sr; 08-15-2022 at 11:53 PM.

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

    Re: Combine data from multiple sheets and remove duplicates if any

    Sub test()
        Dim ws As Worksheet, e, a, x, i As Long, ii As Long
        Application.ScreenUpdating = False
        Set ws = Sheets("expected result")
        ws.Columns("a:b").ClearContents
        ws.[a1:b1] = [{"Code","List"}]
        For Each e In Sheets(Array("L", "P", "A", "M"))  '<--- Add/remove if needed.
            With e.Range("c6", e.Range("c" & Rows.Count).End(xlUp))
                ws.Range("b" & Rows.Count).End(xlUp)(2).Resize(.Rows.Count).Value = .Value
            End With
        Next
        With ws.Range("b2", ws.Range("b" & Rows.Count).End(xlUp))
            .Replace vbLf, "", 2
            a = .Value
            For i = 1 To UBound(a, 1)
                x = Split(a(i, 1), "(")
                If UBound(x) > 0 Then
                    For ii = 1 To UBound(x)
                        If Val(x(ii)) Like String(9, "#") Then
                            a(i, 1) = Val(x(ii)): Exit For
                        End If
                    Next
                End If
            Next
            With .Columns(0).Resize(, 2)
                .Columns(1) = a
                .Sort .Columns(2)
                .RemoveDuplicates 1, 0
            End With
        End With
        Application.ScreenUpdating = True
    End Sub

  9. #9
    Forum Contributor
    Join Date
    03-11-2011
    Location
    Nepal
    MS-Off Ver
    MS Excel 2024
    Posts
    1,390

    Re: Combine data from multiple sheets and remove duplicates if any

    Thanx @JEC & @jindongot the results as expected. Post #1 remaining part adding specific sheet name & sorting alphabetically was accomplished perfectly by Jindon.

  10. #10
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Combine data from multiple sheets and remove duplicates if any

    Post# 3 & 4 doesn't seems to work as per expected results in Post#1
    Please explain for #3 where you get the differance with the expected results.

  11. #11
    Forum Contributor
    Join Date
    03-11-2011
    Location
    Nepal
    MS-Off Ver
    MS Excel 2024
    Posts
    1,390

    Re: Combine data from multiple sheets and remove duplicates if any

    Kindly enclosed the workbook with your code.My Last version of office is 2021.

  12. #12
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Combine data from multiple sheets and remove duplicates if any

    Here my solution, including the sorting and predefined sheetnames.

    Sub jec()
     Dim sh, d, ar, k, i As Long
     Set d = CreateObject("scripting.dictionary")
     
     For Each sh In Sheets(Array("L", "P", "A", "M"))
        ar = sh.Range("C6", sh.Range("C" & Rows.Count).End(xlUp))
        For i = 1 To UBound(ar)
           k = getNum(ar(i, 1))
           d(k) = Array(k, ar(i, 1))
        Next
     Next
     With Sheets("Expected Result").Range("A2").Resize(d.Count, 2)
       .CurrentRegion.Offset(1).ClearContents
       .Value = Application.Index(d.items, 0, 0)
       .Sort .Columns(2)
     End With
    End Sub
    
    
    Function getNum(cell As Variant) As Variant
     With CreateObject("vbscript.regexp")
       .Global = True
       .Pattern = "\d{9}"
        If .test(cell) Then
          getNum = .Execute(cell)(0)
        Else
          getNum = cell
        End If
     End With
    End Function

  13. #13
    Forum Contributor
    Join Date
    03-11-2011
    Location
    Nepal
    MS-Off Ver
    MS Excel 2024
    Posts
    1,390

    Re: Combine data from multiple sheets and remove duplicates if any

    Thanx JEC.

  14. #14
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Combine data from multiple sheets and remove duplicates if any

    You're welcome!

  15. #15
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: Combine data from multiple sheets and remove duplicates if any

    And one with power query

    let
        Source = Table.Combine({L, P, A, M}),
        AddColl = Table.AddColumn(Source, "Aangepast", each List.Transform(Text.ToList([Kolom1]),each if Value.Is(Value.FromText(_), type number) then _ else null)),
        outRes = Table.AddColumn(Table.TransformColumns(AddColl, {"Aangepast", each Text.Combine(List.Transform(_, Text.From)), type text}), "Aangepast.1", each if Text.Length([Aangepast]) >0 then [Aangepast] else [Kolom1]),
        outOrd = Table.Distinct(Table.ReorderColumns(Table.RemoveColumns(outRes,{"Aangepast"}),{"Aangepast.1", "Kolom1"}), {"Aangepast.1"})
    in
        outOrd
    Attached Files Attached Files

  16. #16
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Combine data from multiple sheets and remove duplicates if any

    @paradise2sr

    Kindly enclosed the workbook with your code.My Last version of office is 2021.

    If you are responding out of sequence, it is usually enough just to mention the helper's user name (e.g @Oeldere).


    "Post# 3 & 4 doesn't seems to work as per expected results in Post#1
    Please explain for #3 where you get the differance with the expected results."

    Since you test the code, you can also give the reason what the differance is with the expected result.

    I expect no differance, so I ask you to show it.

  17. #17
    Forum Contributor
    Join Date
    03-11-2011
    Location
    Nepal
    MS-Off Ver
    MS Excel 2024
    Posts
    1,390

    Re: Combine data from multiple sheets and remove duplicates if any

    Pls find enclosed in attachment.There is #Value Error in Column B in Consolidated Sheet on running code as per your Post #3.This was the main reason,why it did not worked in my version of excel.


    Value error.png
    Attached Files Attached Files

  18. #18
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Combine data from multiple sheets and remove duplicates if any

    See my red text in the code.

    "'it adds an @ before the formula and that result in "value"
    'I remove the @ manualy from the formula and copied the date down.
    "


    See the attached file.

  19. #19
    Forum Contributor
    Join Date
    03-11-2011
    Location
    Nepal
    MS-Off Ver
    MS Excel 2024
    Posts
    1,390

    Re: Combine data from multiple sheets and remove duplicates if any

    Oh,Now I got it.Thanx for the clearing the confusion.

  20. #20
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Combine data from multiple sheets and remove duplicates if any

    @paradise2sr

    Thanks for the reply.

  21. #21
    Forum Contributor
    Join Date
    03-11-2011
    Location
    Nepal
    MS-Off Ver
    MS Excel 2024
    Posts
    1,390

    Re: Combine data from multiple sheets and remove duplicates if any

    Hi@Jindon,

    Refer to your Post #8 code,I found some data not mentioned above in Post #1 (enclosed in attachment) instead of extracting number is extracting whole text.

    I have highlighted the with Red color.

    Hence,I hope you can revised your code accordingly. Others things are fine.

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

    Re: Combine data from multiple sheets and remove duplicates if any

    Sub test()
        Dim ws As Worksheet, e, a, v, x, i As Long
        Application.ScreenUpdating = False
        Set ws = Sheets("expected result")
        ws.Columns("a:b").ClearContents
        ws.[a1:b1] = [{"Code","List"}]
        For Each e In Sheets(Array("L", "P", "A", "M"))  '<--- Add/remove if needed.
            With e.Range("c6", e.Range("c" & Rows.Count).End(xlUp))
                ws.Range("b" & Rows.Count).End(xlUp)(2).Resize(.Rows.Count).Value = .Value
            End With
        Next
        With ws.Range("b2", ws.Range("b" & Rows.Count).End(xlUp))
            .Replace vbCrLf, "", 2
            a = .Value
            For i = 1 To UBound(a, 1)
                x = Split(Replace(a(i, 1), ")", ""), "(")
                If UBound(x) > 0 Then
                    For Each e In x
                        For Each v In Split(Replace(e, "-", ","), ",")
                            If Not Trim$(v) Like "*[!0-9]*" Then
                                a(i, 1) = Trim$(v): Exit For
                            End If
                        Next
                    Next
                End If
            Next
            With .Columns(0).Resize(, 2)
                .Columns(1) = a
                .Sort .Columns(2)
                .RemoveDuplicates 1, 0
            End With
        End With
        Application.ScreenUpdating = True
    End Sub

  23. #23
    Forum Contributor
    Join Date
    03-11-2011
    Location
    Nepal
    MS-Off Ver
    MS Excel 2024
    Posts
    1,390

    Re: Combine data from multiple sheets and remove duplicates if any

    Worked well but one of my data earlier was resulted in correct way but now resulted wrong using this code.Kindly see what is missing.
    Attached Files Attached Files

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

    Re: Combine data from multiple sheets and remove duplicates if any

    Is this the last one?

    Don't ask one by one.

  25. #25
    Forum Contributor
    Join Date
    03-11-2011
    Location
    Nepal
    MS-Off Ver
    MS Excel 2024
    Posts
    1,390

    Re: Combine data from multiple sheets and remove duplicates if any

    Yes,it's last one.I have tested in all my private file with large data.

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

    Re: Combine data from multiple sheets and remove duplicates if any

    Add one line in bold
        With ws.Range("b2", ws.Range("b" & Rows.Count).End(xlUp))
            .Replace vbCrLf, "", 2
            .Replace vbTab, "", 2
            a = .Value

  27. #27
    Forum Contributor
    Join Date
    03-11-2011
    Location
    Nepal
    MS-Off Ver
    MS Excel 2024
    Posts
    1,390

    Re: Combine data from multiple sheets and remove duplicates if any

    Perfectly worked with no issue.Thanx

    Sub test()
        Dim ws As Worksheet, e, a, v, x, i As Long
        Application.ScreenUpdating = False
        Set ws = Sheets("expected result")
        ws.Columns("a:b").ClearContents
        ws.[a1:b1] = [{"Code","List"}]
        For Each e In Sheets(Array("L", "P", "A", "M"))  '<--- Add/remove if needed.
            With e.Range("c6", e.Range("c" & Rows.Count).End(xlUp))
                ws.Range("b" & Rows.Count).End(xlUp)(2).Resize(.Rows.Count).Value = .Value
            End With
        Next
        With ws.Range("b2", ws.Range("b" & Rows.Count).End(xlUp))
            .Replace vbCrLf, "", 2
            .Replace vbTab, "", 2
            a = .Value
            For i = 1 To UBound(a, 1)
                x = Split(Replace(a(i, 1), ")", ""), "(")
                If UBound(x) > 0 Then
                    For Each e In x
                        For Each v In Split(Replace(e, "-", ","), ",")
                            If Not Trim$(v) Like "*[!0-9]*" Then
                                a(i, 1) = Trim$(v): Exit For
                            End If
                        Next
                    Next
                End If
            Next
            With .Columns(0).Resize(, 2)
                .Columns(1) = a
                .Sort .Columns(2)
                .RemoveDuplicates 1, 0
            End With
        End With
        Application.ScreenUpdating = True
    End Sub
    Last edited by paradise2sr; 08-21-2022 at 09:27 AM.

+ 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. Combine data through different worksheets and remove duplicates
    By Haree in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-05-2020, 10:27 AM
  2. Replies: 6
    Last Post: 10-19-2018, 02:44 PM
  3. Need to combine data from two different sheets and avoid duplicates
    By samratpahwa in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-17-2018, 11:48 PM
  4. Replies: 1
    Last Post: 05-03-2018, 05:18 PM
  5. Select Data from Multiple Sheets, Save on one - Remove Duplicates
    By snuffnchess in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-02-2018, 11:35 AM
  6. Need a macro to Compare multiple sheets and Remove Duplicates
    By ajaypal.sp in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-03-2015, 12:18 PM
  7. Replies: 2
    Last Post: 01-13-2014, 10:52 AM

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