Transpose Macro Needed (willing to donate to charity for quick answer!)

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
I have sheet 1 with 2 columns, a number in A and a transmission in column B. I need the number and the transmission transposed onto sheet 2. There may be a unique number in A or several the same with different transmissions in B. In the example I have highlighted the duplicates in A. The yellow one has the same number in A but 3 transmissions in B so they will go under the transmission header in B (also yellow), the same with blue and orange.

Sheet A will consist of several hundred thousand rows so this is just a small example. Like I mentioned I would appreciate a quick solution on this one as it is for work so willing to donate to charity if needs be. Thanks.


Excel 2010
AB
1NumberTransmission
210200362MTM
310200363MTM
410200364MTM
510200364ATM
610200364CVT
710200365MTM
810200365ATM
910200366MTM
1010200371MTM
1110200371SEMI
1210200372ATM
1310200373ATM
Sheet1


Sheet 2 after code.


Excel 2010
ABCDEFGHIJKLMNOP
1ATMATM & CVTATM & CVT & MTMATM & CVT & MTM & SemiATM & CVT & SemiATM & MTMATM & MTM & SemiATM & SemiCVTCVT & MTMCVT & MTM & SemiCVT & SemiMTMMTM & SemiSemiDirect
21020037210200364102003651020036210200371
31020037310200363
Sheet2
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
What are the range of values that column B will cover?
We already have

MTM
CVT
ATM
Semi

It looks like there's a Direct too.
Will there be others as well or is that the complete range of values?
 
Last edited:
Upvote 0
Yes those 4 plus Direct, I think I put all the possible combinations in sheet 2. (Direct will always be on its own)
 
Upvote 0
Is Sheet2 already set up or should the macro add it? I have a solution that creates it from scratch ...

WBD
 
Upvote 0
OK. Give this a whirl:

Code:
Public Sub TransposeTransmissions()

Dim lastRow As Long
Dim thisRow As Long
Dim nextRow As Long
Dim nextCol As Long
Dim newSheet As Worksheet
Dim thisSheet As Worksheet
Dim transmissions As Collection
Dim lastNumber As String
Dim columnHeader As String
Dim foundColumn
Dim i As Long

' Turn of screen updating
Application.ScreenUpdating = False

' Set up the first sheet and find the last row
Set thisSheet = ActiveSheet ' or Set thisSheet = Sheets("Sheet1")
lastRow = thisSheet.Cells(thisSheet.Rows.Count, "A").End(xlUp).Row + 1

' Set up the second sheet
Set newSheet = Sheets.Add(After:=thisSheet) ' or Set newSheet = Sheets("Sheet2")

' Loop through all data
lastNumber = ""
nextCol = 1
For thisRow = 2 To lastRow
    ' Change of number?
    If thisSheet.Cells(thisRow, "A").Value <> lastNumber Then
        If lastNumber <> "" Then
            ' Something to put in the second sheet - create the column header
            columnHeader = ""
            For i = 1 To transmissions.Count
                columnHeader = columnHeader & " & " & transmissions(i)
            Next i
            columnHeader = Mid(columnHeader, 4)
            
            ' Look for this column
            foundColumn = Application.Match(columnHeader, newSheet.Range("1:1"), 0)
            
            ' Add a new column if we didn't find it
            If IsError(foundColumn) Then
                newSheet.Cells(1, nextCol).Value = columnHeader
                newSheet.Cells(2, nextCol).Value = lastNumber
                nextCol = nextCol + 1
            Else
                ' Find the last row in the found column and add the number there
                nextRow = newSheet.Cells(newSheet.Rows.Count, foundColumn).End(xlUp).Row + 1
                newSheet.Cells(nextRow, foundColumn).Value = lastNumber
            End If
        End If
        
        ' Remember this number and create a new collection of transmissions
        lastNumber = thisSheet.Cells(thisRow, "A").Value
        Set transmissions = New Collection
    End If
    
    ' Add this transmission to the collection
    InsertTransmission transmissions, thisSheet.Cells(thisRow, "B").Value
Next thisRow

' Turn on screen updating
Application.ScreenUpdating = True

End Sub
Private Sub InsertTransmission(transmissions As Collection, transmission As String)

Dim i As Long

' If the collection is empty then just add it
If transmissions.Count = 0 Then
    transmissions.Add Item:=transmission
    Exit Sub
End If

' Put it in the right place
For i = 1 To transmissions.Count
    If transmission < transmissions(i) Then
        transmissions.Add Item:=transmission, Before:=i
        Exit For
    End If
Next i

' If we didn't find a place for it then put it at the end
If i > transmissions.Count Then transmissions.Add Item:=transmission

End Sub

That should create the new sheet and populate the columns. When done, you might need to sort the data on the second sheet to put the columns in alphabetical order. Highlight the data and click the Sort button (on the Data ribbon). Click Options... and select "Sort left to right" then click OK. Now select to sort by Row 1 and click OK.

WBD
 
Upvote 0
Thanks, I am not sure it is working correct. I am getting results like below and the headers stretch right across to column EA?


Excel 2010
ABCDEFG
1CVTDirect DriveMTMMTM & MTM & MTMMTM & MTMMTM & MTM & MTM & MTMMTM & MTM & SEMI & SEMI
2M10100770000001M10105420000003M10200350000001M10200350000004M10200350000005M10200370000001M10200370000005
3M10100900000001M10105420000006M10200350000002M10200360000004M10200360000002M10200370000003M10200400000008
4M10100900000002M10105420000007M10200350000003M10200390000007M10200360000005M10200370000004M10200400000019
5M10100900000003M10105420000008M10200350000006M10200390000008M10200370000002M10200370000008M10204210000002
Sheet4
 
Upvote 0
Oh! Is it possible that M10200350000004 has three entries in the list each one with a transmission of "MTM"? Try this subtle change instead?

Code:
Public Sub TransposeTransmissions()

Dim lastRow As Long
Dim thisRow As Long
Dim nextRow As Long
Dim nextCol As Long
Dim newSheet As Worksheet
Dim thisSheet As Worksheet
Dim transmissions As Collection
Dim lastNumber As String
Dim columnHeader As String
Dim foundColumn
Dim i As Long

' Turn of screen updating
Application.ScreenUpdating = False

' Set up the first sheet and find the last row
Set thisSheet = ActiveSheet ' or Set thisSheet = Sheets("Sheet1")
lastRow = thisSheet.Cells(thisSheet.Rows.Count, "A").End(xlUp).Row + 1

' Set up the second sheet
Set newSheet = Sheets.Add(After:=thisSheet) ' or Set newSheet = Sheets("Sheet2")

' Loop through all data
lastNumber = ""
nextCol = 1
For thisRow = 2 To lastRow
    ' Change of number?
    If thisSheet.Cells(thisRow, "A").Value <> lastNumber Then
        If lastNumber <> "" Then
            ' Something to put in the second sheet - create the column header
            columnHeader = ""
            For i = 1 To transmissions.Count
                columnHeader = columnHeader & " & " & transmissions(i)
            Next i
            columnHeader = Mid(columnHeader, 4)
            
            ' Look for this column
            foundColumn = Application.Match(columnHeader, newSheet.Range("1:1"), 0)
            
            ' Add a new column if we didn't find it
            If IsError(foundColumn) Then
                newSheet.Cells(1, nextCol).Value = columnHeader
                newSheet.Cells(2, nextCol).Value = lastNumber
                nextCol = nextCol + 1
            Else
                ' Find the last row in the found column and add the number there
                nextRow = newSheet.Cells(newSheet.Rows.Count, foundColumn).End(xlUp).Row + 1
                newSheet.Cells(nextRow, foundColumn).Value = lastNumber
            End If
        End If
        
        ' Remember this number and create a new collection of transmissions
        lastNumber = thisSheet.Cells(thisRow, "A").Value
        Set transmissions = New Collection
    End If
    
    ' Add this transmission to the collection
    InsertTransmission transmissions, thisSheet.Cells(thisRow, "B").Value
Next thisRow

' Turn on screen updating
Application.ScreenUpdating = True

End Sub
Private Sub InsertTransmission(transmissions As Collection, transmission As String)

Dim i As Long

' If the collection is empty then just add it
If transmissions.Count = 0 Then
    transmissions.Add Item:=transmission
    Exit Sub
End If

' Put it in the right place
For i = 1 To transmissions.Count
    If transmission < transmissions(i) Then
        transmissions.Add Item:=transmission, Before:=i
        Exit For
    ElseIf transmission = transmissions(i) Then
        i = 1
        Exit For
    End If
Next i

' If we didn't find a place for it then put it at the end
If i > transmissions.Count Then transmissions.Add Item:=transmission

End Sub

WBD
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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