Excel/Concatenate List

Advertisement


Question
QUESTION: Hello Tom,

You successfully helped me with a previous question and was hoping you could help me with another.

I have list of school campuses, each containing one or more schools.  I would like to rearrange the information so that each school is listed horizontally across a table with each school in a separate column as opposed to a separate row. The problem is the way the information is organized doesn't allow this to be easily done.  Here is an example of how the information is organized now:

CAMPUS  SCHOOL_NAME
111     AAA
111     BBB
111     CCC
111     DDD
222     EEE
333     FFF
333     GGG
333     HHH


I would like to rearrange the school name information as follows:

CAMPUS  SCHOOL1  SCHOOL2   SCHOOL3 SCHOOL4
111     AAA      BBB       CCC     DDD
222     EEE          
333     FFF      GGG       HHH

I'm hoping that some sort of macro containing a loop with an if statement might be the trick.  Any information on how to solve this would be greatly appreciated.  Thank you.

ANSWER: assume

Sheet1
a1:  111
b1:  AAA

Output on sheet2

Sub ABC()
Dim sh1 As Worksheet, sh2 As Worksheet
Dim rw As Long, col As Long
Dim r As Range, cell As Range
Set sh1 = Worksheets("Sheet1")
Set sh2 = Worksheets("Sheet2")
rw = 1
col = 2
Set r = sh1.Range("A1", sh1.Cells(sh1.Rows.Count, 1).End(xlUp))
For Each cell In r
If cell.Row = 1 Then
  sh2.Cells(1, 1) = cell
  sh2.Cells(1, 2) = cell.Offset(0, 1)
  rw = 1
  col = 2
ElseIf cell <> cell.Offset(-1, 0) Then
  rw = rw + 1
  col = 2
  sh2.Cells(rw, 1) = cell
  sh2.Cells(rw, 2) = cell.Offset(0, 1)
Else
  col = col + 1
  sh2.Cells(rw, col) = cell.Offset(0, 1)
End If
Next cell
End Sub

worked for me.

Test it on a copy of your workbook.

--
Regards,
Tom Ogilvy



---------- FOLLOW-UP ----------

QUESTION: Hey Tom,

Can you provide a macro to do the reverse.  So I am starting out with the following:

CAMPUS  SCHOOL1  SCHOOL2   SCHOOL3 SCHOOL4
111     AAA      BBB       CCC     DDD
222     EEE          
333     FFF      GGG       HHH

and I want the information organized like such:

CAMPUS  SCHOOL_NAME
111     AAA
111     BBB
111     CCC
111     DDD
222     EEE
333     FFF
333     GGG
333     HHH

Any help is very much appreciated.  Thank you!

Answer
Enrique Garcia

The macro assumes the data you show is on a sheet named Sheet1 with the first 111 in cell A2
It writes the data to a sheet named Sheet2 and starts in row 2 and writes in columns A and B.

It writes the headers in row 1

Test this in a copy of your workbook.

Sub ABC1()
Dim sh1 As Worksheet, sh2 As Worksheet
Dim rw As Long, r1 As Range
Dim r As Range, cell As Range
Set sh1 = Worksheets("Sheet1")
Set sh2 = Worksheets("Sheet2")
sh2.Range("A1") = "CAMPUS"
sh2.Range("B1") = "SCHOOL_NAME"
rw = 2
Set r = sh1.Range("A2", sh1.Cells(sh1.Rows.Count, 1).End(xlUp))
For Each cell In r
Set r1 = sh1.Cells(cell.Row, 2)
Do While Len(Trim(r1)) > 0
  sh2.Cells(rw, 1) = cell
  sh2.Cells(rw, 2) = r1
  rw = rw + 1
  Set r1 = r1.Offset(0, 1)
Loop

Next cell
End Sub

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.