Converting Columns into new rows with repeating data

pamelaamm

New Member
Joined
Nov 18, 2014
Messages
3
I have a table that looks like this.
DealerName
DealerPhone
DealerAddress
DealerCity
DealerState
DealerZip
DealerEmail
BuildingName1
BuildingAddress1
BuildingCity1
BuildingState1
BuildingZip1
BuildingName2
BuildingAddress2
BuildingCity2
BuildingState2
BuildingZip2
UnneededColumn1
Date
UnneededColumn2
UnneededColumn3
CorpID
PD Consulting
555-555-5555
1234 Consulting Drive
Cleveland
OH
44145
consulting@email.com
PDBuilding1
111 Address Ave
Westlake
OH
44145
Consulting2
222 Address Dr
North Olmsted
OH
44070
xxx
11/3/2014
zzz
yyy
12345
Michel Wireless
222-222-2222
2222 Wireless Way
Bay Village
OH
44076
wireless@email.com
WirelessBuilding1
111 Cell St
Solon
OH
44139
bbb
10/3/2014
rrr
ttt
45677

<tbody>
</tbody>


My problem I'm trying to solve for is to have a macro run that will take specified cells in a row (dealer information) and duplicate to a new row for each building that is listed in the columns. My table only shows 2 building data sets, however my actual spreadsheet holds 15. If building columns are left blank, I want the macro to skip to the next entry. Here is what I'd like the above data to look like:
Date
BuildingName
BuildingAddress
BuildingCity
BuildingState
BuildingZip
CorpID
DealerName
DealerAddress
DealerCity
DealerState
DealerZip
DealerEmail
DealerPhone
11/3/2014
PDBuilding1
111 Address Ave
Westlake
OH
44145
12345
PDConsulting
1234 Consulting Drive
Cleveland
OH
44145
consulting@email.com
555-555-5555
11/3/2014
Consulting2
222 Address Dr
North Olmsted
OH
44070
12345
PDConsulting
1234 Consulting Drive
Cleveland
OH
44145
consulting@email.com
555-555-5555
10/3/2014
WirelessBuilding1
111 Cell St
Solon
OH
44139
45677
Michel Wireless
2222 Wireless Way
Bay Village
OH
44076
wireless@email.com
222-222-2222

<tbody>
</tbody>


So as you can see, it's a bit complicated in the sense that certain cells within a row need to repeat, columns need to split into new rows, blanks need to be ignored, and certain columns can also be ignored. I'm very new to VBA, so I've been doing a lot of research and have found the following solution works, but I cannot manipulate it enough to get the outcome desired. Any help is greatly greatly appreciated!! Thanks in advance!
Here is what I've been trying to manipulate:

Sub ShrinkTable()
Dim maxRows As Double
Dim maxCols As Integer
Dim data As Variant maxRows = Cells(1, 1).End(xlDown).row
maxCols = Cells(1, 1).End(xlToRight).Column data = Range(Cells(1, 1), Cells(maxRows, maxCols))

Dim newSht As Worksheet
Set newSht = Sheets.Add

With newSht

.Cells(1, 1).Value = "Name"
.Cells(1, 2).Value = "Column"

Dim writeRow As Double writeRow = 2

Dim row As Double
row = 2
Dim col As Integer

Do While True

col = 2
Do While True
If data(row, col) = "" Then Exit Do 'Skip Blanks

'Name
.Cells(writeRow, 1).Value = data(row, 1)

'Language
.Cells(writeRow, 2).Value = data(row, col)

writeRow = writeRow + 1
If col = maxCols Then Exit Do 'Exit clause
col = col + 1
Loop

If row = maxRows Then Exit Do 'exit clause
row = row + 1
Loop

End With
End Sub
EXAMPLE:
| A | B | C | D |
+-------+------------+------------+------------+
1 | Name | Language 1 | Language 2 | Language 3 |
+=======+============+============+============+
2 | John | English | Chinese | Spanish |
3 | Wendy | Chinese | French | English |
4 | Peter | Spanish | Chinese | English |<br><br>And I want to generate a table that has only one language column. The other two language columns should become new rows like this:

| A | B |
+-------+----------+
1 | Name | Language |
+=======+==========+
2 | John | English |
3 | John | Chinese |
4 | John | Spanish |
5 | Wendy | Chinese |
6 | Wendy | French |
7 | Wendy | English |
8 | Peter | Spanish |
9 | Peter | Chinese |
10 | Peter | English |
 
Last edited:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Below are the input and output data


Excel 2010
ABCD
1NameLanguage1Language2Language3
2JohnEnglishChineseSpanish
3WendyChineseFrenchEnglish
4PeterSpanishChineseEnglish
Sheet1



Excel 2010
AB
1NameLanguage
2JohnEnglish
3JohnChinese
4JohnSpanish
5WendyChinese
6WendyFrench
7WendyEnglish
8PeterSpanish
9PeterChinese
10PeterEnglish
Sheet2

Code:
Sub RearrangeData()
    Dim wks As Worksheet, wks2 As Worksheet
    Dim I As Integer, j As Integer, lrow As Integer
    
    Set wks = Worksheets("Sheet1")
    Set wks2 = Worksheets("Sheet2")
    
    With wks2
        .Range("A1:B1") = Array("Name", "Language")
    lrow = 2
    
    For I = 2 To wks.Range("A" & Rows.Count).End(xlUp).Row
        For j = 2 To wks.Cells(I, Columns.Count).End(xlToLeft).Column
            .Cells(lrow, 1) = wks.Cells(I, 1)
            .Cells(lrow, 2) = wks.Cells(I, j)
            lrow = lrow + 1
        Next j
    Next I
    End With
End Sub

The code assumes original data is in Sheet1 and the output is in sheet2, change as necessary
 
Upvote 0
Thank you so much for the reply - but I need code for fixing the first table. The second was just a simpler example. Anyway you could help with the first two tables shown?
 
Upvote 0
<p>
Definitely one of the most non-elegant codes i have written, but have a go at it</p>
 
Last edited:
Upvote 0
Code:
Sub Rearrangedata()
    Dim wks As Worksheet, wks2 As Worksheet
    Dim I As Integer, j As Integer
    Dim lrow As Integer, K As Integer
    
    Set wks = Worksheets("Sheet1")
    Set wks2 = Worksheets("Sheet2")
    
    lrow = 2
    
    With wks2
    For I = 2 To wks.Range("A" & Rows.Count).End(xlUp).Row
        If wks.Range("M" & I).Value <> "" Then
            j = 2
        Else
            j = 1
        End If
        For K = 1 To j
                .Range("G" & lrow) = wks.Range("V" & I).Value
                .Range("H" & lrow) = wks.Range("A" & I).Value
                .Range("I" & lrow) = wks.Range("C" & I).Value
                .Range("J" & lrow) = wks.Range("D" & I).Value
                .Range("K" & lrow) = wks.Range("E" & I).Value
                .Range("L" & lrow) = wks.Range("F" & I).Value
                .Range("M" & lrow) = wks.Range("G" & I).Value
                .Range("N" & lrow) = wks.Range("B" & I).Value
            If K = 1 Then
                .Range("A" & lrow) = wks.Range("S" & I).Value
                .Range("B" & lrow) = wks.Range("H" & I).Value
                .Range("C" & lrow) = wks.Range("I" & I).Value
                .Range("D" & lrow) = wks.Range("J" & I).Value
                .Range("E" & lrow) = wks.Range("K" & I).Value
                .Range("F" & lrow) = wks.Range("L" & I).Value
                lrow = lrow + 1
            Else
                .Range("A" & lrow) = wks.Range("S" & I).Value
                .Range("B" & lrow) = wks.Range("M" & I).Value
                .Range("C" & lrow) = wks.Range("N" & I).Value
                .Range("D" & lrow) = wks.Range("O" & I).Value
                .Range("E" & lrow) = wks.Range("P" & I).Value
                .Range("F" & lrow) = wks.Range("Q" & I).Value
                lrow = lrow + 1
            End If
        Next K
    Next I
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,530
Messages
6,125,353
Members
449,220
Latest member
Edwin_SVRZ

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