Excel - transpose rows (differently sized groups) to columns









up vote
1
down vote

favorite
1












I have some excel data which are now in rows and I want to get them into columns in an easy an effective way and I am not able to figure out how to do it. Any advice will be welcome! Thanks.



Example: turn sth like this in Excel



Team A
John
Team B
Peter
John
Team C
John
Peter
Oliver
Anna
Team D
Anna


into:



Team A John
Team B Peter John
Team C John Peter Oliver Anna
Team D Anna









share|improve this question





















  • So using the original list it will be possible to distinguish the teams which are to go into column A from the names? If so, how? I'm guessing in reality they aren't all called 'Team A', 'Team B', etc.
    – XOR LX
    Nov 10 at 21:03











  • Will they be ordered in team blocks as shown and does the terms Team A etc exist as mentioned above?
    – QHarr
    Nov 10 at 21:49















up vote
1
down vote

favorite
1












I have some excel data which are now in rows and I want to get them into columns in an easy an effective way and I am not able to figure out how to do it. Any advice will be welcome! Thanks.



Example: turn sth like this in Excel



Team A
John
Team B
Peter
John
Team C
John
Peter
Oliver
Anna
Team D
Anna


into:



Team A John
Team B Peter John
Team C John Peter Oliver Anna
Team D Anna









share|improve this question





















  • So using the original list it will be possible to distinguish the teams which are to go into column A from the names? If so, how? I'm guessing in reality they aren't all called 'Team A', 'Team B', etc.
    – XOR LX
    Nov 10 at 21:03











  • Will they be ordered in team blocks as shown and does the terms Team A etc exist as mentioned above?
    – QHarr
    Nov 10 at 21:49













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I have some excel data which are now in rows and I want to get them into columns in an easy an effective way and I am not able to figure out how to do it. Any advice will be welcome! Thanks.



Example: turn sth like this in Excel



Team A
John
Team B
Peter
John
Team C
John
Peter
Oliver
Anna
Team D
Anna


into:



Team A John
Team B Peter John
Team C John Peter Oliver Anna
Team D Anna









share|improve this question













I have some excel data which are now in rows and I want to get them into columns in an easy an effective way and I am not able to figure out how to do it. Any advice will be welcome! Thanks.



Example: turn sth like this in Excel



Team A
John
Team B
Peter
John
Team C
John
Peter
Oliver
Anna
Team D
Anna


into:



Team A John
Team B Peter John
Team C John Peter Oliver Anna
Team D Anna






excel rows transpose






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 20:53









Adam

83




83











  • So using the original list it will be possible to distinguish the teams which are to go into column A from the names? If so, how? I'm guessing in reality they aren't all called 'Team A', 'Team B', etc.
    – XOR LX
    Nov 10 at 21:03











  • Will they be ordered in team blocks as shown and does the terms Team A etc exist as mentioned above?
    – QHarr
    Nov 10 at 21:49

















  • So using the original list it will be possible to distinguish the teams which are to go into column A from the names? If so, how? I'm guessing in reality they aren't all called 'Team A', 'Team B', etc.
    – XOR LX
    Nov 10 at 21:03











  • Will they be ordered in team blocks as shown and does the terms Team A etc exist as mentioned above?
    – QHarr
    Nov 10 at 21:49
















So using the original list it will be possible to distinguish the teams which are to go into column A from the names? If so, how? I'm guessing in reality they aren't all called 'Team A', 'Team B', etc.
– XOR LX
Nov 10 at 21:03





So using the original list it will be possible to distinguish the teams which are to go into column A from the names? If so, how? I'm guessing in reality they aren't all called 'Team A', 'Team B', etc.
– XOR LX
Nov 10 at 21:03













Will they be ordered in team blocks as shown and does the terms Team A etc exist as mentioned above?
– QHarr
Nov 10 at 21:49





Will they be ordered in team blocks as shown and does the terms Team A etc exist as mentioned above?
– QHarr
Nov 10 at 21:49













2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










I'm guessing your real data is much longer than this list so here is what i would do in that case.



First, put the list in column B and add a formula that copies the Team down column A:



[1]: https://i.stack.imgur.com/hDWJO.png



*note you have to copy and paste the value from b2 into a2 and start the formula on a3.
Type the formula =IF(LEFT(B3,4)="Team",B3,A2) in cell a3 and drag it down (or control shift down then control d to fill down). What is this formula doing? It looks at the B cell, if it starts with "Team" it uses the value of that cell, otherwise it uses the value of the cell above (which will be another "Team").



Then, copy and paste and values column A so you don't loose the formula results after the next steps:



enter image description here



Filter column B "player" on the search term "team" and delete those entire rows:



enter image description here



Now you have column A of teams, Column B of players and use this formula in column C: =IF(A2=A1,CONCATENATE(C1," ",B2),CONCATENATE(A2," ",B2)).
This formula looks at the Team column and if it differs, it start a new chain of team and player otherwise it adds the player to the chain above of team and player.



enter image description here



I hope you can follow the logic here and accomplish what you are trying to do. Let me know how it goes.






share|improve this answer




















  • Simple, efficient and works! Thanks a lot!
    – Adam
    Nov 11 at 11:28










  • You welcome, I'm glad you like it. Don't forget to up vote!
    – pablokimon
    Nov 12 at 6:39

















up vote
0
down vote













Column to Vertical List



Sample



Option Explicit

'*******************************************************************************
' Purpose: Processes a one-column range containing groups of title-values data,
' transposing the titles to the first column of a range and the values
' to columns next to the title thus creating a vertical list.
'*******************************************************************************

Sub ColumnToVerticalList()

Const cStrSheet As String = "Sheet1" ' Worksheet Name
Const cLngFirstRow As Long = 2 ' First Row of Source Data
Const cStrColumn As String = "A" ' Column of Source Data
Const cStrSearch As String = "Team" ' Search String
Const cStrCell As String = "C2" ' Target Cell

Dim arrSource As Variant ' Source Array
Dim lngArr As Long ' Source Array Row Counter

Dim arrTarget As Variant ' Target Array
Dim lngRows As Long ' Number of Rows (Counter) in Target Array
Dim iCols As Integer ' Number of Columns (Counter) in Target Array
Dim iColsTemp As Integer ' Target Array Columns Counter
Dim strTargetRange As String ' Target Range

' Paste the calculated source range into the source array - arrSource.
With ThisWorkbook.Worksheets(cStrSheet)
arrSource = .Range( _
.Cells(cLngFirstRow, cStrColumn), _
.Cells(.Cells(Rows.Count, cStrColumn).End(xlUp).Row, cStrColumn))
End With

' Calculate the number of rows and columns of the target array - arrTarget.
iColsTemp = 1
For lngArr = LBound(arrSource) To UBound(arrSource)
If InStr(1, arrSource(lngArr, 1), cStrSearch, vbTextCompare) <> 0 Then
If iColsTemp > iCols Then
iCols = iColsTemp
End If
iColsTemp = 1
Debug.Print arrSource(lngArr, 1)
lngRows = lngRows + 1
Else
iColsTemp = iColsTemp + 1
End If
Next

' Calculate the target range address.
strTargetRange = Range(Cells(Range(cStrCell).Row, Range(cStrCell).Column), _
Cells(Range(cStrCell).Row + lngRows - 1, _
Range(cStrCell).Column + iCols - 1)).Address

' Resize the target array.
ReDim arrTarget(1 To lngRows, 1 To iCols)

' Write data from source array to target array.
lngRows = 0
iCols = 1
For lngArr = LBound(arrSource) To UBound(arrSource)
If InStr(1, arrSource(lngArr, 1), cStrSearch, vbTextCompare) <> 0 Then
iCols = 1
lngRows = lngRows + 1
arrTarget(lngRows, 1) = arrSource(lngArr, 1)
Else
iCols = iCols + 1
arrTarget(lngRows, iCols) = arrSource(lngArr, 1)
End If
Next

' Paste data of the target array into the target range
ThisWorkbook.Worksheets(cStrSheet).Range(strTargetRange) = arrTarget

End Sub





share|improve this answer






















  • Thanks a lot. I will definitely try this the next time as it seems a really good way to learn more about VBAs.
    – Adam
    Nov 11 at 11:29










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243312%2fexcel-transpose-rows-differently-sized-groups-to-columns%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










I'm guessing your real data is much longer than this list so here is what i would do in that case.



First, put the list in column B and add a formula that copies the Team down column A:



[1]: https://i.stack.imgur.com/hDWJO.png



*note you have to copy and paste the value from b2 into a2 and start the formula on a3.
Type the formula =IF(LEFT(B3,4)="Team",B3,A2) in cell a3 and drag it down (or control shift down then control d to fill down). What is this formula doing? It looks at the B cell, if it starts with "Team" it uses the value of that cell, otherwise it uses the value of the cell above (which will be another "Team").



Then, copy and paste and values column A so you don't loose the formula results after the next steps:



enter image description here



Filter column B "player" on the search term "team" and delete those entire rows:



enter image description here



Now you have column A of teams, Column B of players and use this formula in column C: =IF(A2=A1,CONCATENATE(C1," ",B2),CONCATENATE(A2," ",B2)).
This formula looks at the Team column and if it differs, it start a new chain of team and player otherwise it adds the player to the chain above of team and player.



enter image description here



I hope you can follow the logic here and accomplish what you are trying to do. Let me know how it goes.






share|improve this answer




















  • Simple, efficient and works! Thanks a lot!
    – Adam
    Nov 11 at 11:28










  • You welcome, I'm glad you like it. Don't forget to up vote!
    – pablokimon
    Nov 12 at 6:39














up vote
0
down vote



accepted










I'm guessing your real data is much longer than this list so here is what i would do in that case.



First, put the list in column B and add a formula that copies the Team down column A:



[1]: https://i.stack.imgur.com/hDWJO.png



*note you have to copy and paste the value from b2 into a2 and start the formula on a3.
Type the formula =IF(LEFT(B3,4)="Team",B3,A2) in cell a3 and drag it down (or control shift down then control d to fill down). What is this formula doing? It looks at the B cell, if it starts with "Team" it uses the value of that cell, otherwise it uses the value of the cell above (which will be another "Team").



Then, copy and paste and values column A so you don't loose the formula results after the next steps:



enter image description here



Filter column B "player" on the search term "team" and delete those entire rows:



enter image description here



Now you have column A of teams, Column B of players and use this formula in column C: =IF(A2=A1,CONCATENATE(C1," ",B2),CONCATENATE(A2," ",B2)).
This formula looks at the Team column and if it differs, it start a new chain of team and player otherwise it adds the player to the chain above of team and player.



enter image description here



I hope you can follow the logic here and accomplish what you are trying to do. Let me know how it goes.






share|improve this answer




















  • Simple, efficient and works! Thanks a lot!
    – Adam
    Nov 11 at 11:28










  • You welcome, I'm glad you like it. Don't forget to up vote!
    – pablokimon
    Nov 12 at 6:39












up vote
0
down vote



accepted







up vote
0
down vote



accepted






I'm guessing your real data is much longer than this list so here is what i would do in that case.



First, put the list in column B and add a formula that copies the Team down column A:



[1]: https://i.stack.imgur.com/hDWJO.png



*note you have to copy and paste the value from b2 into a2 and start the formula on a3.
Type the formula =IF(LEFT(B3,4)="Team",B3,A2) in cell a3 and drag it down (or control shift down then control d to fill down). What is this formula doing? It looks at the B cell, if it starts with "Team" it uses the value of that cell, otherwise it uses the value of the cell above (which will be another "Team").



Then, copy and paste and values column A so you don't loose the formula results after the next steps:



enter image description here



Filter column B "player" on the search term "team" and delete those entire rows:



enter image description here



Now you have column A of teams, Column B of players and use this formula in column C: =IF(A2=A1,CONCATENATE(C1," ",B2),CONCATENATE(A2," ",B2)).
This formula looks at the Team column and if it differs, it start a new chain of team and player otherwise it adds the player to the chain above of team and player.



enter image description here



I hope you can follow the logic here and accomplish what you are trying to do. Let me know how it goes.






share|improve this answer












I'm guessing your real data is much longer than this list so here is what i would do in that case.



First, put the list in column B and add a formula that copies the Team down column A:



[1]: https://i.stack.imgur.com/hDWJO.png



*note you have to copy and paste the value from b2 into a2 and start the formula on a3.
Type the formula =IF(LEFT(B3,4)="Team",B3,A2) in cell a3 and drag it down (or control shift down then control d to fill down). What is this formula doing? It looks at the B cell, if it starts with "Team" it uses the value of that cell, otherwise it uses the value of the cell above (which will be another "Team").



Then, copy and paste and values column A so you don't loose the formula results after the next steps:



enter image description here



Filter column B "player" on the search term "team" and delete those entire rows:



enter image description here



Now you have column A of teams, Column B of players and use this formula in column C: =IF(A2=A1,CONCATENATE(C1," ",B2),CONCATENATE(A2," ",B2)).
This formula looks at the Team column and if it differs, it start a new chain of team and player otherwise it adds the player to the chain above of team and player.



enter image description here



I hope you can follow the logic here and accomplish what you are trying to do. Let me know how it goes.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 7:16









pablokimon

564




564











  • Simple, efficient and works! Thanks a lot!
    – Adam
    Nov 11 at 11:28










  • You welcome, I'm glad you like it. Don't forget to up vote!
    – pablokimon
    Nov 12 at 6:39
















  • Simple, efficient and works! Thanks a lot!
    – Adam
    Nov 11 at 11:28










  • You welcome, I'm glad you like it. Don't forget to up vote!
    – pablokimon
    Nov 12 at 6:39















Simple, efficient and works! Thanks a lot!
– Adam
Nov 11 at 11:28




Simple, efficient and works! Thanks a lot!
– Adam
Nov 11 at 11:28












You welcome, I'm glad you like it. Don't forget to up vote!
– pablokimon
Nov 12 at 6:39




You welcome, I'm glad you like it. Don't forget to up vote!
– pablokimon
Nov 12 at 6:39












up vote
0
down vote













Column to Vertical List



Sample



Option Explicit

'*******************************************************************************
' Purpose: Processes a one-column range containing groups of title-values data,
' transposing the titles to the first column of a range and the values
' to columns next to the title thus creating a vertical list.
'*******************************************************************************

Sub ColumnToVerticalList()

Const cStrSheet As String = "Sheet1" ' Worksheet Name
Const cLngFirstRow As Long = 2 ' First Row of Source Data
Const cStrColumn As String = "A" ' Column of Source Data
Const cStrSearch As String = "Team" ' Search String
Const cStrCell As String = "C2" ' Target Cell

Dim arrSource As Variant ' Source Array
Dim lngArr As Long ' Source Array Row Counter

Dim arrTarget As Variant ' Target Array
Dim lngRows As Long ' Number of Rows (Counter) in Target Array
Dim iCols As Integer ' Number of Columns (Counter) in Target Array
Dim iColsTemp As Integer ' Target Array Columns Counter
Dim strTargetRange As String ' Target Range

' Paste the calculated source range into the source array - arrSource.
With ThisWorkbook.Worksheets(cStrSheet)
arrSource = .Range( _
.Cells(cLngFirstRow, cStrColumn), _
.Cells(.Cells(Rows.Count, cStrColumn).End(xlUp).Row, cStrColumn))
End With

' Calculate the number of rows and columns of the target array - arrTarget.
iColsTemp = 1
For lngArr = LBound(arrSource) To UBound(arrSource)
If InStr(1, arrSource(lngArr, 1), cStrSearch, vbTextCompare) <> 0 Then
If iColsTemp > iCols Then
iCols = iColsTemp
End If
iColsTemp = 1
Debug.Print arrSource(lngArr, 1)
lngRows = lngRows + 1
Else
iColsTemp = iColsTemp + 1
End If
Next

' Calculate the target range address.
strTargetRange = Range(Cells(Range(cStrCell).Row, Range(cStrCell).Column), _
Cells(Range(cStrCell).Row + lngRows - 1, _
Range(cStrCell).Column + iCols - 1)).Address

' Resize the target array.
ReDim arrTarget(1 To lngRows, 1 To iCols)

' Write data from source array to target array.
lngRows = 0
iCols = 1
For lngArr = LBound(arrSource) To UBound(arrSource)
If InStr(1, arrSource(lngArr, 1), cStrSearch, vbTextCompare) <> 0 Then
iCols = 1
lngRows = lngRows + 1
arrTarget(lngRows, 1) = arrSource(lngArr, 1)
Else
iCols = iCols + 1
arrTarget(lngRows, iCols) = arrSource(lngArr, 1)
End If
Next

' Paste data of the target array into the target range
ThisWorkbook.Worksheets(cStrSheet).Range(strTargetRange) = arrTarget

End Sub





share|improve this answer






















  • Thanks a lot. I will definitely try this the next time as it seems a really good way to learn more about VBAs.
    – Adam
    Nov 11 at 11:29














up vote
0
down vote













Column to Vertical List



Sample



Option Explicit

'*******************************************************************************
' Purpose: Processes a one-column range containing groups of title-values data,
' transposing the titles to the first column of a range and the values
' to columns next to the title thus creating a vertical list.
'*******************************************************************************

Sub ColumnToVerticalList()

Const cStrSheet As String = "Sheet1" ' Worksheet Name
Const cLngFirstRow As Long = 2 ' First Row of Source Data
Const cStrColumn As String = "A" ' Column of Source Data
Const cStrSearch As String = "Team" ' Search String
Const cStrCell As String = "C2" ' Target Cell

Dim arrSource As Variant ' Source Array
Dim lngArr As Long ' Source Array Row Counter

Dim arrTarget As Variant ' Target Array
Dim lngRows As Long ' Number of Rows (Counter) in Target Array
Dim iCols As Integer ' Number of Columns (Counter) in Target Array
Dim iColsTemp As Integer ' Target Array Columns Counter
Dim strTargetRange As String ' Target Range

' Paste the calculated source range into the source array - arrSource.
With ThisWorkbook.Worksheets(cStrSheet)
arrSource = .Range( _
.Cells(cLngFirstRow, cStrColumn), _
.Cells(.Cells(Rows.Count, cStrColumn).End(xlUp).Row, cStrColumn))
End With

' Calculate the number of rows and columns of the target array - arrTarget.
iColsTemp = 1
For lngArr = LBound(arrSource) To UBound(arrSource)
If InStr(1, arrSource(lngArr, 1), cStrSearch, vbTextCompare) <> 0 Then
If iColsTemp > iCols Then
iCols = iColsTemp
End If
iColsTemp = 1
Debug.Print arrSource(lngArr, 1)
lngRows = lngRows + 1
Else
iColsTemp = iColsTemp + 1
End If
Next

' Calculate the target range address.
strTargetRange = Range(Cells(Range(cStrCell).Row, Range(cStrCell).Column), _
Cells(Range(cStrCell).Row + lngRows - 1, _
Range(cStrCell).Column + iCols - 1)).Address

' Resize the target array.
ReDim arrTarget(1 To lngRows, 1 To iCols)

' Write data from source array to target array.
lngRows = 0
iCols = 1
For lngArr = LBound(arrSource) To UBound(arrSource)
If InStr(1, arrSource(lngArr, 1), cStrSearch, vbTextCompare) <> 0 Then
iCols = 1
lngRows = lngRows + 1
arrTarget(lngRows, 1) = arrSource(lngArr, 1)
Else
iCols = iCols + 1
arrTarget(lngRows, iCols) = arrSource(lngArr, 1)
End If
Next

' Paste data of the target array into the target range
ThisWorkbook.Worksheets(cStrSheet).Range(strTargetRange) = arrTarget

End Sub





share|improve this answer






















  • Thanks a lot. I will definitely try this the next time as it seems a really good way to learn more about VBAs.
    – Adam
    Nov 11 at 11:29












up vote
0
down vote










up vote
0
down vote









Column to Vertical List



Sample



Option Explicit

'*******************************************************************************
' Purpose: Processes a one-column range containing groups of title-values data,
' transposing the titles to the first column of a range and the values
' to columns next to the title thus creating a vertical list.
'*******************************************************************************

Sub ColumnToVerticalList()

Const cStrSheet As String = "Sheet1" ' Worksheet Name
Const cLngFirstRow As Long = 2 ' First Row of Source Data
Const cStrColumn As String = "A" ' Column of Source Data
Const cStrSearch As String = "Team" ' Search String
Const cStrCell As String = "C2" ' Target Cell

Dim arrSource As Variant ' Source Array
Dim lngArr As Long ' Source Array Row Counter

Dim arrTarget As Variant ' Target Array
Dim lngRows As Long ' Number of Rows (Counter) in Target Array
Dim iCols As Integer ' Number of Columns (Counter) in Target Array
Dim iColsTemp As Integer ' Target Array Columns Counter
Dim strTargetRange As String ' Target Range

' Paste the calculated source range into the source array - arrSource.
With ThisWorkbook.Worksheets(cStrSheet)
arrSource = .Range( _
.Cells(cLngFirstRow, cStrColumn), _
.Cells(.Cells(Rows.Count, cStrColumn).End(xlUp).Row, cStrColumn))
End With

' Calculate the number of rows and columns of the target array - arrTarget.
iColsTemp = 1
For lngArr = LBound(arrSource) To UBound(arrSource)
If InStr(1, arrSource(lngArr, 1), cStrSearch, vbTextCompare) <> 0 Then
If iColsTemp > iCols Then
iCols = iColsTemp
End If
iColsTemp = 1
Debug.Print arrSource(lngArr, 1)
lngRows = lngRows + 1
Else
iColsTemp = iColsTemp + 1
End If
Next

' Calculate the target range address.
strTargetRange = Range(Cells(Range(cStrCell).Row, Range(cStrCell).Column), _
Cells(Range(cStrCell).Row + lngRows - 1, _
Range(cStrCell).Column + iCols - 1)).Address

' Resize the target array.
ReDim arrTarget(1 To lngRows, 1 To iCols)

' Write data from source array to target array.
lngRows = 0
iCols = 1
For lngArr = LBound(arrSource) To UBound(arrSource)
If InStr(1, arrSource(lngArr, 1), cStrSearch, vbTextCompare) <> 0 Then
iCols = 1
lngRows = lngRows + 1
arrTarget(lngRows, 1) = arrSource(lngArr, 1)
Else
iCols = iCols + 1
arrTarget(lngRows, iCols) = arrSource(lngArr, 1)
End If
Next

' Paste data of the target array into the target range
ThisWorkbook.Worksheets(cStrSheet).Range(strTargetRange) = arrTarget

End Sub





share|improve this answer














Column to Vertical List



Sample



Option Explicit

'*******************************************************************************
' Purpose: Processes a one-column range containing groups of title-values data,
' transposing the titles to the first column of a range and the values
' to columns next to the title thus creating a vertical list.
'*******************************************************************************

Sub ColumnToVerticalList()

Const cStrSheet As String = "Sheet1" ' Worksheet Name
Const cLngFirstRow As Long = 2 ' First Row of Source Data
Const cStrColumn As String = "A" ' Column of Source Data
Const cStrSearch As String = "Team" ' Search String
Const cStrCell As String = "C2" ' Target Cell

Dim arrSource As Variant ' Source Array
Dim lngArr As Long ' Source Array Row Counter

Dim arrTarget As Variant ' Target Array
Dim lngRows As Long ' Number of Rows (Counter) in Target Array
Dim iCols As Integer ' Number of Columns (Counter) in Target Array
Dim iColsTemp As Integer ' Target Array Columns Counter
Dim strTargetRange As String ' Target Range

' Paste the calculated source range into the source array - arrSource.
With ThisWorkbook.Worksheets(cStrSheet)
arrSource = .Range( _
.Cells(cLngFirstRow, cStrColumn), _
.Cells(.Cells(Rows.Count, cStrColumn).End(xlUp).Row, cStrColumn))
End With

' Calculate the number of rows and columns of the target array - arrTarget.
iColsTemp = 1
For lngArr = LBound(arrSource) To UBound(arrSource)
If InStr(1, arrSource(lngArr, 1), cStrSearch, vbTextCompare) <> 0 Then
If iColsTemp > iCols Then
iCols = iColsTemp
End If
iColsTemp = 1
Debug.Print arrSource(lngArr, 1)
lngRows = lngRows + 1
Else
iColsTemp = iColsTemp + 1
End If
Next

' Calculate the target range address.
strTargetRange = Range(Cells(Range(cStrCell).Row, Range(cStrCell).Column), _
Cells(Range(cStrCell).Row + lngRows - 1, _
Range(cStrCell).Column + iCols - 1)).Address

' Resize the target array.
ReDim arrTarget(1 To lngRows, 1 To iCols)

' Write data from source array to target array.
lngRows = 0
iCols = 1
For lngArr = LBound(arrSource) To UBound(arrSource)
If InStr(1, arrSource(lngArr, 1), cStrSearch, vbTextCompare) <> 0 Then
iCols = 1
lngRows = lngRows + 1
arrTarget(lngRows, 1) = arrSource(lngArr, 1)
Else
iCols = iCols + 1
arrTarget(lngRows, iCols) = arrSource(lngArr, 1)
End If
Next

' Paste data of the target array into the target range
ThisWorkbook.Worksheets(cStrSheet).Range(strTargetRange) = arrTarget

End Sub






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 7:16

























answered Nov 11 at 6:57









VBasic2008

592211




592211











  • Thanks a lot. I will definitely try this the next time as it seems a really good way to learn more about VBAs.
    – Adam
    Nov 11 at 11:29
















  • Thanks a lot. I will definitely try this the next time as it seems a really good way to learn more about VBAs.
    – Adam
    Nov 11 at 11:29















Thanks a lot. I will definitely try this the next time as it seems a really good way to learn more about VBAs.
– Adam
Nov 11 at 11:29




Thanks a lot. I will definitely try this the next time as it seems a really good way to learn more about VBAs.
– Adam
Nov 11 at 11:29

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243312%2fexcel-transpose-rows-differently-sized-groups-to-columns%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党