Hello Guys,
I want to transpose data with 5 Columns to only 2 and a new row should be created for every column.
Source Data:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Berlin[/TD]
[TD]1000[/TD]
[TD]2000[/TD]
[TD]3000[/TD]
[TD]4000[/TD]
[TD]5000[/TD]
[/TR]
[TR]
[TD]Dresden[/TD]
[TD]500[/TD]
[TD]300[/TD]
[TD]300[/TD]
[TD]300[/TD]
[TD]300[/TD]
[/TR]
</tbody>[/TABLE]
After the macro, the data should be transferred to this result:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Berlin[/TD]
[TD]1000[/TD]
[/TR]
[TR]
[TD]Berlin[/TD]
[TD]2000[/TD]
[/TR]
[TR]
[TD]Berlin[/TD]
[TD]3000[/TD]
[/TR]
[TR]
[TD]Berlin[/TD]
[TD]4000[/TD]
[/TR]
[TR]
[TD]Berlin[/TD]
[TD]5000[/TD]
[/TR]
[TR]
[TD]Dresden[/TD]
[TD]500[/TD]
[/TR]
[TR]
[TD]Dresden[/TD]
[TD]300[/TD]
[/TR]
[TR]
[TD]Dresden[/TD]
[TD]300[/TD]
[/TR]
[TR]
[TD]Dresden[/TD]
[TD]300[/TD]
[/TR]
[TR]
[TD]Dresden[/TD]
[TD]300[/TD]
[/TR]
</tbody>[/TABLE]
So one line per data. I found a code online, which works only for up to 3 Columns (I have 5), but I am not able to adjust it for 5 columns:
Can anyone help me out there? I think I miss to enhance several lines of the code, since I always get strange results.
Thanks a lot and best
Brandoe
I want to transpose data with 5 Columns to only 2 and a new row should be created for every column.
Source Data:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Berlin[/TD]
[TD]1000[/TD]
[TD]2000[/TD]
[TD]3000[/TD]
[TD]4000[/TD]
[TD]5000[/TD]
[/TR]
[TR]
[TD]Dresden[/TD]
[TD]500[/TD]
[TD]300[/TD]
[TD]300[/TD]
[TD]300[/TD]
[TD]300[/TD]
[/TR]
</tbody>[/TABLE]
After the macro, the data should be transferred to this result:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Berlin[/TD]
[TD]1000[/TD]
[/TR]
[TR]
[TD]Berlin[/TD]
[TD]2000[/TD]
[/TR]
[TR]
[TD]Berlin[/TD]
[TD]3000[/TD]
[/TR]
[TR]
[TD]Berlin[/TD]
[TD]4000[/TD]
[/TR]
[TR]
[TD]Berlin[/TD]
[TD]5000[/TD]
[/TR]
[TR]
[TD]Dresden[/TD]
[TD]500[/TD]
[/TR]
[TR]
[TD]Dresden[/TD]
[TD]300[/TD]
[/TR]
[TR]
[TD]Dresden[/TD]
[TD]300[/TD]
[/TR]
[TR]
[TD]Dresden[/TD]
[TD]300[/TD]
[/TR]
[TR]
[TD]Dresden[/TD]
[TD]300[/TD]
[/TR]
</tbody>[/TABLE]
So one line per data. I found a code online, which works only for up to 3 Columns (I have 5), but I am not able to adjust it for 5 columns:
Code:
Sub TransposeInsertRows()
Dim xRg As Range
Dim i As Long, j As Long, k As Long
Dim x As Long, y As Long
Set xRg = Application.InputBox _
(Prompt:="Range Selection...", _
Title:="Range Selection", Type:=8)
Application.ScreenUpdating = False
x = xRg(1, 1).Column + 2
y = xRg(1, xRg.Columns.Count).Column
For i = xRg(xRg.Rows.Count, 1).Row To xRg(1, 1).Row Step -1
If Cells(i, x) <> "" And Cells(i, x + 1) <> "" Then
k = Cells(i, x - 2).End(xlToRight).Column
If k > y Then k = y
For j = k To x + 1 Step -1
Cells(i + 1, 1).EntireRow.Insert
With Cells(i + 1, x - 2)
.Value = .Offset(-1, 0)
.Offset(0, 1) = .Offset(-1, 1)
.Offset(0, 2) = Cells(i, j)
End With
Cells(i, j).ClearContents
Next j
End If
Next i
Application.ScreenUpdating = True
End Sub
Can anyone help me out there? I think I miss to enhance several lines of the code, since I always get strange results.
Thanks a lot and best
Brandoe
Last edited by a moderator: