Consolidate different worksheets in one and delete duplicates

vinzent

New Member
Joined
Feb 7, 2012
Messages
35
Office Version
  1. 365
Platform
  1. Windows
Hello
I am doing a macro to get some information, but I have next problem on this part:
I want to consolidate the column A from multiple worksheets in one worksheet (CONSOLIDATED TAB) but I have a problem due the the sheets contain different names and the information to consolidate starting when the title is "Cost Elements" until the end and I need to remove duplicated only to show one consolidate list and delete the rows showing "Debit" and "Over/Under"
If can help me with this will be great excellent. Thank You
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I have some doubts,
how many columns are going to be consolidated? only column "A"?
the data "Debit" and "Over/Under" are in column "A"?
 
Upvote 0
I have some doubts,
how many columns are going to be consolidated? only column "A"?
the data "Debit" and "Over/Under" are in column "A"?
Yes, only the column to consolidate is "A" and both words are in column "A"
 
Upvote 0
Try this:

VBA Code:
Sub consolidate_A()
  Dim dic As Object
  Dim i As Long, j As Long, k As Long, lr As Long
  Dim a() As Variant, b As Variant
  Dim sh As Worksheet, shc As Worksheet
  Dim f As Range
  
  Set shc = Sheets("CONSOLIDATED")
  Set dic = CreateObject("Scripting.Dictionary")
  dic.comparemode = vbTextCompare
  For Each sh In Sheets
    lr = lr + sh.Range("A" & Rows.Count).End(3).Row
  Next
  ReDim b(1 To lr, 1 To Columns("A").Column)    '
  shc.Cells.ClearContents
  
  For Each sh In Sheets
    Select Case sh.Name
      Case shc.Name, "report", "etc"  'fit sheet names that don't go in consolidation
      Case Else
        Erase a
        Set f = sh.Range("A:A").Find("Cost Elements", , xlValues, xlWhole, , , False)
        If Not f Is Nothing Then
          a = sh.Range("A" & f.Row + 1, sh.Cells(sh.Range("A" & Rows.Count).End(3).Row + 3, UBound(b, 2))).Value
          For i = 1 To UBound(a, 1)
            If a(i, 1) <> "" And a(i, 1) <> "Debit" And a(i, 1) <> "Over/Under" Then
              If Not dic.exists(a(i, 1)) Then
                dic(a(i, 1)) = Empty
                k = k + 1
                For j = 1 To UBound(a, 2)
                  b(k, j) = a(i, j)
                Next
              End If
            End If
          Next
        End If
    End Select
  Next
  shc.Range("A2").Resize(k, UBound(b, 2)).Value = b
End Sub
 
Upvote 0
Solution
Try this:

VBA Code:
Sub consolidate_A()
  Dim dic As Object
  Dim i As Long, j As Long, k As Long, lr As Long
  Dim a() As Variant, b As Variant
  Dim sh As Worksheet, shc As Worksheet
  Dim f As Range
 
  Set shc = Sheets("CONSOLIDATED")
  Set dic = CreateObject("Scripting.Dictionary")
  dic.comparemode = vbTextCompare
  For Each sh In Sheets
    lr = lr + sh.Range("A" & Rows.Count).End(3).Row
  Next
  ReDim b(1 To lr, 1 To Columns("A").Column)    '
  shc.Cells.ClearContents
 
  For Each sh In Sheets
    Select Case sh.Name
      Case shc.Name, "report", "etc"  'fit sheet names that don't go in consolidation
      Case Else
        Erase a
        Set f = sh.Range("A:A").Find("Cost Elements", , xlValues, xlWhole, , , False)
        If Not f Is Nothing Then
          a = sh.Range("A" & f.Row + 1, sh.Cells(sh.Range("A" & Rows.Count).End(3).Row + 3, UBound(b, 2))).Value
          For i = 1 To UBound(a, 1)
            If a(i, 1) <> "" And a(i, 1) <> "Debit" And a(i, 1) <> "Over/Under" Then
              If Not dic.exists(a(i, 1)) Then
                dic(a(i, 1)) = Empty
                k = k + 1
                For j = 1 To UBound(a, 2)
                  b(k, j) = a(i, j)
                Next
              End If
            End If
          Next
        End If
    End Select
  Next
  shc.Range("A2").Resize(k, UBound(b, 2)).Value = b
End Sub

[/QUOTE]
This code is working.. thank you. You are amazing!!
 
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,558
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