Consolidate data with same text values

vixell23

New Member
Joined
Oct 12, 2015
Messages
7
Hi I need a code that would consolidate data that have the same non-amount values. To illustrate:

A B C
X Y 10
X Y 15
G H 12
G H 5
D S 3

Would then be consolidated as:

A B C
X Y 25
G H 17
D S 3

The original file would have hundreds of columna and thousands of rows.

Thanks in advance!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
concatnum
xy10xy10
xy15xy15
gh12gh12
gh5gh5
ds3ds3
Sum of num
concatTotal
xy25
gh17
ds3
a helper table out of the way and a pivot tableGrand Total45
if needed you can reformat the pivot to show x and y in separate columns

<colgroup><col span="9"><col><col><col span="2"></colgroup><tbody>
</tbody>
 
Upvote 0
Try this code in a copy of your workbook. I'm assuming that the data has headings as in my example. If that is not the case, post back for modifications.

Rich (BB code):
Sub Consol()
  Dim cols As Long, i As Long
  Dim d As Object
  Dim data As Variant, vals As Variant
  Dim combo As String
  
  Application.ScreenUpdating = False
  Set d = CreateObject("scripting.dictionary")
  d.CompareMode = vbTextCompare
  With Range("A1").CurrentRegion
    cols = .Columns.Count
    .Resize(, cols - 1).AdvancedFilter _
        Action:=xlFilterCopy, CopyToRange:=.Cells(1, cols + 2), Unique:=True
    data = .Resize(, cols - 1).Value
    vals = .Columns(cols).Value
    For i = 1 To UBound(data, 1)
      combo = Join(Application.Index(data, i, 0), "|")
      If d.exists(combo) Then
        d.Item(combo) = d.Item(combo) + vals(i, 1)
      Else
        d.Add combo, vals(i, 1)
      End If
    Next i
    .Cells(1, cols * 2 + 1).Resize(d.Count, 1).Value = Application.Transpose(d.items)
  End With
  Application.ScreenUpdating = True
End Sub


Given the original data in A1:D7 below, the code produced what you see in columns F:I

Excel Workbook
ABCDEFGHI
1Data 1Data 2Data 3ValueData 1Data 2Data 3Value
2XYZ10XYZ25
3XYZ15GHA12
4GHA12GHQ5
5GHQ5DSS8
6DSS3
7DSS5
8
Consolidate
 
Upvote 0
peter - he wants gh to be consolidated as well - I think.........ah no 3rd column differs - I did not read the exam question.........
 
Upvote 0
Try this code in a copy of your workbook. I'm assuming that the data has headings as in my example. If that is not the case, post back for modifications.

Rich (BB code):
Sub Consol()
  Dim cols As Long, i As Long
  Dim d As Object
  Dim data As Variant, vals As Variant
  Dim combo As String
  
  Application.ScreenUpdating = False
  Set d = CreateObject("scripting.dictionary")
  d.CompareMode = vbTextCompare
  With Range("A1").CurrentRegion
    cols = .Columns.Count
    .Resize(, cols - 1).AdvancedFilter _
        Action:=xlFilterCopy, CopyToRange:=.Cells(1, cols + 2), Unique:=True
    data = .Resize(, cols - 1).Value
    vals = .Columns(cols).Value
    For i = 1 To UBound(data, 1)
      combo = Join(Application.Index(data, i, 0), "|")
      If d.exists(combo) Then
        d.Item(combo) = d.Item(combo) + vals(i, 1)
      Else
        d.Add combo, vals(i, 1)
      End If
    Next i
    .Cells(1, cols * 2 + 1).Resize(d.Count, 1).Value = Application.Transpose(d.items)
  End With
  Application.ScreenUpdating = True
End Sub


Given the original data in A1:D7 below, the code produced what you see in columns F:I

Excel Workbook
ABCDEFGHI
1Data 1Data 2Data 3ValueData 1Data 2Data 3Value
2XYZ10XYZ25
3XYZ15GHA12
4GHA12GHQ5
5GHQ5DSS8
6DSS3
7DSS5
8
Consolidate

Hi Peter! Thank you very much for this. Would it be possible for the resulting new matrix to be shown in another worksheet?
 
Upvote 0
Hi Peter! Thank you very much for this. Would it be possible for the resulting new matrix to be shown in another worksheet?
Yes

Does that other worksheet already exist in the workbook?

If so,
- what is its name?
- is there anything already on it that needs to be kept? If so, what/where?
 
Upvote 0
Yes

Does that other worksheet already exist in the workbook?

If so,
- what is its name?
- is there anything already on it that needs to be kept? If so, what/where?

No there's no soecific worksheet. What I would like is for it to create a new worksheet in the same workbook containing the consolidated data. Again, thank you very much!
 
Upvote 0
No there's no soecific worksheet. What I would like is for it to create a new worksheet in the same workbook containing the consolidated data. Again, thank you very much!
Assuming the original data sheet is the active sheet when the code is run, try this.
Rich (BB code):
Sub Consol_2()
  Dim cols As Long, i As Long
  Dim d As Object
  Dim data As Variant, vals As Variant
  Dim combo As String
  Dim wsOld As Worksheet, wsNew As Worksheet
  
  Application.ScreenUpdating = False
  Set wsOld = ActiveSheet
  Set wsNew = Sheets.Add(After:=wsOld)
  Set d = CreateObject("scripting.dictionary")
  d.CompareMode = vbTextCompare
  With wsOld.Range("A1").CurrentRegion
    cols = .Columns.Count
    .Resize(, cols - 1).AdvancedFilter _
        Action:=xlFilterCopy, CopyToRange:=wsNew.Range("A1"), Unique:=True
    data = .Resize(, cols - 1).Value
    vals = .Columns(cols).Value
    For i = 1 To UBound(data, 1)
      combo = Join(Application.Index(data, i, 0), "|")
      If d.exists(combo) Then
        d.Item(combo) = d.Item(combo) + vals(i, 1)
      Else
        d.Add combo, vals(i, 1)
      End If
    Next i
    wsNew.Cells(1, cols).Resize(d.Count, 1).Value = Application.Transpose(d.items)
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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