I have a table that looks like this. [TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]DealerName
[/TD]
[TD]DealerPhone
[/TD]
[TD]DealerAddress
[/TD]
[TD]DealerCity
[/TD]
[TD]DealerState
[/TD]
[TD]DealerZip
[/TD]
[TD]DealerEmail
[/TD]
[TD]BuildingName1
[/TD]
[TD]BuildingAddress1
[/TD]
[TD]BuildingCity1
[/TD]
[TD]BuildingState1
[/TD]
[TD]BuildingZip1
[/TD]
[TD]BuildingName2
[/TD]
[TD]BuildingAddress2
[/TD]
[TD]BuildingCity2
[/TD]
[TD]BuildingState2
[/TD]
[TD]BuildingZip2
[/TD]
[TD]UnneededColumn1
[/TD]
[TD]Date
[/TD]
[TD]UnneededColumn2
[/TD]
[TD]UnneededColumn3
[/TD]
[TD]CorpID
[/TD]
[/TR]
[TR]
[TD]PD Consulting
[/TD]
[TD]555-555-5555
[/TD]
[TD]1234 Consulting Drive
[/TD]
[TD]Cleveland
[/TD]
[TD]OH
[/TD]
[TD]44145
[/TD]
[TD]consulting@email.com
[/TD]
[TD]PDBuilding1
[/TD]
[TD]111 Address Ave
[/TD]
[TD]Westlake
[/TD]
[TD]OH
[/TD]
[TD]44145
[/TD]
[TD]Consulting2
[/TD]
[TD]222 Address Dr
[/TD]
[TD]North Olmsted
[/TD]
[TD]OH
[/TD]
[TD]44070
[/TD]
[TD]xxx
[/TD]
[TD]11/3/2014
[/TD]
[TD]zzz
[/TD]
[TD]yyy
[/TD]
[TD]12345
[/TD]
[/TR]
[TR]
[TD]Michel Wireless
[/TD]
[TD]222-222-2222
[/TD]
[TD]2222 Wireless Way
[/TD]
[TD]Bay Village
[/TD]
[TD]OH
[/TD]
[TD]44076
[/TD]
[TD]wireless@email.com
[/TD]
[TD]WirelessBuilding1
[/TD]
[TD]111 Cell St
[/TD]
[TD]Solon
[/TD]
[TD]OH
[/TD]
[TD]44139
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]bbb
[/TD]
[TD]10/3/2014
[/TD]
[TD]rrr
[/TD]
[TD]ttt
[/TD]
[TD]45677
[/TD]
[/TR]
</tbody>[/TABLE]
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:[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Date
[/TD]
[TD]BuildingName
[/TD]
[TD]BuildingAddress
[/TD]
[TD]BuildingCity
[/TD]
[TD]BuildingState
[/TD]
[TD]BuildingZip
[/TD]
[TD]CorpID
[/TD]
[TD]DealerName
[/TD]
[TD]DealerAddress
[/TD]
[TD]DealerCity
[/TD]
[TD]DealerState
[/TD]
[TD]DealerZip
[/TD]
[TD]DealerEmail
[/TD]
[TD]DealerPhone
[/TD]
[/TR]
[TR]
[TD]11/3/2014
[/TD]
[TD]PDBuilding1
[/TD]
[TD]111 Address Ave
[/TD]
[TD]Westlake
[/TD]
[TD]OH
[/TD]
[TD]44145
[/TD]
[TD]12345
[/TD]
[TD]PDConsulting
[/TD]
[TD]1234 Consulting Drive
[/TD]
[TD]Cleveland
[/TD]
[TD]OH
[/TD]
[TD]44145
[/TD]
[TD]consulting@email.com
[/TD]
[TD]555-555-5555
[/TD]
[/TR]
[TR]
[TD]11/3/2014
[/TD]
[TD]Consulting2
[/TD]
[TD]222 Address Dr
[/TD]
[TD]North Olmsted
[/TD]
[TD]OH
[/TD]
[TD]44070
[/TD]
[TD]12345
[/TD]
[TD]PDConsulting
[/TD]
[TD]1234 Consulting Drive
[/TD]
[TD]Cleveland
[/TD]
[TD]OH
[/TD]
[TD]44145
[/TD]
[TD]consulting@email.com
[/TD]
[TD]555-555-5555
[/TD]
[/TR]
[TR]
[TD]10/3/2014
[/TD]
[TD]WirelessBuilding1
[/TD]
[TD]111 Cell St
[/TD]
[TD]Solon
[/TD]
[TD]OH
[/TD]
[TD]44139
[/TD]
[TD]45677
[/TD]
[TD]Michel Wireless
[/TD]
[TD]2222 Wireless Way
[/TD]
[TD]Bay Village
[/TD]
[TD]OH
[/TD]
[TD]44076
[/TD]
[TD]wireless@email.com
[/TD]
[TD]222-222-2222
[/TD]
[/TR]
</tbody>[/TABLE]
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 |
<tbody>[TR]
[TD]DealerName
[/TD]
[TD]DealerPhone
[/TD]
[TD]DealerAddress
[/TD]
[TD]DealerCity
[/TD]
[TD]DealerState
[/TD]
[TD]DealerZip
[/TD]
[TD]DealerEmail
[/TD]
[TD]BuildingName1
[/TD]
[TD]BuildingAddress1
[/TD]
[TD]BuildingCity1
[/TD]
[TD]BuildingState1
[/TD]
[TD]BuildingZip1
[/TD]
[TD]BuildingName2
[/TD]
[TD]BuildingAddress2
[/TD]
[TD]BuildingCity2
[/TD]
[TD]BuildingState2
[/TD]
[TD]BuildingZip2
[/TD]
[TD]UnneededColumn1
[/TD]
[TD]Date
[/TD]
[TD]UnneededColumn2
[/TD]
[TD]UnneededColumn3
[/TD]
[TD]CorpID
[/TD]
[/TR]
[TR]
[TD]PD Consulting
[/TD]
[TD]555-555-5555
[/TD]
[TD]1234 Consulting Drive
[/TD]
[TD]Cleveland
[/TD]
[TD]OH
[/TD]
[TD]44145
[/TD]
[TD]consulting@email.com
[/TD]
[TD]PDBuilding1
[/TD]
[TD]111 Address Ave
[/TD]
[TD]Westlake
[/TD]
[TD]OH
[/TD]
[TD]44145
[/TD]
[TD]Consulting2
[/TD]
[TD]222 Address Dr
[/TD]
[TD]North Olmsted
[/TD]
[TD]OH
[/TD]
[TD]44070
[/TD]
[TD]xxx
[/TD]
[TD]11/3/2014
[/TD]
[TD]zzz
[/TD]
[TD]yyy
[/TD]
[TD]12345
[/TD]
[/TR]
[TR]
[TD]Michel Wireless
[/TD]
[TD]222-222-2222
[/TD]
[TD]2222 Wireless Way
[/TD]
[TD]Bay Village
[/TD]
[TD]OH
[/TD]
[TD]44076
[/TD]
[TD]wireless@email.com
[/TD]
[TD]WirelessBuilding1
[/TD]
[TD]111 Cell St
[/TD]
[TD]Solon
[/TD]
[TD]OH
[/TD]
[TD]44139
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]bbb
[/TD]
[TD]10/3/2014
[/TD]
[TD]rrr
[/TD]
[TD]ttt
[/TD]
[TD]45677
[/TD]
[/TR]
</tbody>[/TABLE]
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:[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Date
[/TD]
[TD]BuildingName
[/TD]
[TD]BuildingAddress
[/TD]
[TD]BuildingCity
[/TD]
[TD]BuildingState
[/TD]
[TD]BuildingZip
[/TD]
[TD]CorpID
[/TD]
[TD]DealerName
[/TD]
[TD]DealerAddress
[/TD]
[TD]DealerCity
[/TD]
[TD]DealerState
[/TD]
[TD]DealerZip
[/TD]
[TD]DealerEmail
[/TD]
[TD]DealerPhone
[/TD]
[/TR]
[TR]
[TD]11/3/2014
[/TD]
[TD]PDBuilding1
[/TD]
[TD]111 Address Ave
[/TD]
[TD]Westlake
[/TD]
[TD]OH
[/TD]
[TD]44145
[/TD]
[TD]12345
[/TD]
[TD]PDConsulting
[/TD]
[TD]1234 Consulting Drive
[/TD]
[TD]Cleveland
[/TD]
[TD]OH
[/TD]
[TD]44145
[/TD]
[TD]consulting@email.com
[/TD]
[TD]555-555-5555
[/TD]
[/TR]
[TR]
[TD]11/3/2014
[/TD]
[TD]Consulting2
[/TD]
[TD]222 Address Dr
[/TD]
[TD]North Olmsted
[/TD]
[TD]OH
[/TD]
[TD]44070
[/TD]
[TD]12345
[/TD]
[TD]PDConsulting
[/TD]
[TD]1234 Consulting Drive
[/TD]
[TD]Cleveland
[/TD]
[TD]OH
[/TD]
[TD]44145
[/TD]
[TD]consulting@email.com
[/TD]
[TD]555-555-5555
[/TD]
[/TR]
[TR]
[TD]10/3/2014
[/TD]
[TD]WirelessBuilding1
[/TD]
[TD]111 Cell St
[/TD]
[TD]Solon
[/TD]
[TD]OH
[/TD]
[TD]44139
[/TD]
[TD]45677
[/TD]
[TD]Michel Wireless
[/TD]
[TD]2222 Wireless Way
[/TD]
[TD]Bay Village
[/TD]
[TD]OH
[/TD]
[TD]44076
[/TD]
[TD]wireless@email.com
[/TD]
[TD]222-222-2222
[/TD]
[/TR]
</tbody>[/TABLE]
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: