How do I combine rows, combining duplicates as well as concatenati

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to combine rows of data and I need a certain column to concatenate
only if it is in a group of rows that have duplicate information in another
column.

Example:

Before...

Column 1 Column 2
X A
X B
Y C
Y D

Desired After...

Column 1 Column 2
X AB
Y CD

This is for a spreadsheet with thousands of rows

Any suggestions?
 
Donovan
Your example shows 2 cells in Column 1 which are the same, and then you
concatenate the corresponding 2 cells in Column 2. Can your data have more
than 2 cells in Column 1 with the same value? If so, do you want to
concatenate all the corresponding cells in Column 2?
HTH Otto
 
Maybe a macro????

Option Explicit
Sub testme01()

Dim curWks As Worksheet
Dim newWks As Worksheet
Dim FirstRow As Long
Dim LastRow As Long
Dim iRow As Long
Dim oRow As Long
Dim PrevKey As String
Dim TempStr As String

Set curWks = Worksheets("sheet1")
Set newWks = Worksheets.Add

With curWks
FirstRow = 1
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

oRow = 1
TempStr = ""

'do the first
PrevKey = .Cells(FirstRow, "A").Value
TempStr = .Cells(FirstRow, "B").Value

'do the rest
For iRow = FirstRow + 1 To LastRow + 1
'lastrow + 1 makes sure last entry is processed
If .Cells(iRow, "A").Value = PrevKey Then
TempStr = TempStr & .Cells(iRow, "B").Value
Else
newWks.Cells(oRow, "A").Value = PrevKey
newWks.Cells(oRow, "B").Value = TempStr
PrevKey = .Cells(iRow, "A").Value
TempStr = .Cells(iRow, "B").Value
oRow = oRow + 1
End If
Next iRow
End With

End Sub

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
 
Thanks for the reply Otto. It looks like someone else at our office figured
everything out, so don't worry about it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top