Transpose Code Needed Please For Work.

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,783
Office Version
  1. 365
Platform
  1. Windows
I have sheet 1 as laid out below. I need a code please that will transpose onto sheet 2 and give the example results on sheet 2.

Sheet 1 Before Code


Excel 2010
ABCDE
1STCODEALT5_CODEALT10_CODEALT11_CODEALT12_CODE
2EXDN603122624NIS6032,38634P4261,NS53105,01.9217051.24.07,32.7004,44.002.83
3EXCN201301236,01019CIT2014
4EXCN603901011,01145,01033CIT6040
5EXCN303501010CIT3036
Sheet1


Sheet 2 After Code


Excel 2010
ABC
1Comparative NameComparative Part No.Your Part No.
2ALT10_CODE38634EXDN6031
3ALT10_CODENIS6032EXDN6031
4ALT10_CODECIT2014EXCN2013
5ALT10_CODECIT3036EXCN3035
6ALT10_CODECIT6040EXCN6039
7ALT11_CODE1.92170EXDN6031
8ALT11_CODENS53105EXDN6031
9ALT11_CODEP4261EXDN6031
10ALT12_CODE32.7004EXDN6031
11ALT12_CODE44.002.83EXDN6031
12ALT12_CODE51.24.07EXDN6031
13ALT5_CODE01010EXCN3035
14ALT5_CODE01011EXCN6039
15ALT5_CODE01019EXCN2013
16ALT5_CODE01033EXCN6039
17ALT5_CODE01145EXCN6039
18ALT5_CODE01236EXCN2013
19ALT5_CODE22624EXDN6031
Sheet2


As you can see the code has put 3 headings on sheet 2. When a number is separated by a comma a row is added for each number where I have highlighted in yellow as an example. A little tricky to explain but please contact me for clarification of anything.

On sheet 1 there may be more columns with the same sort of data up to a possible 20 columns.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Give this a shot:

Code:
Public Sub TransposeData()

Dim lastRow As Long
Dim lastCol As Long
Dim thisRow As Long
Dim thisCol As Long
Dim nextRow As Long
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim allValues As Variant
Dim thisValue As Variant

' Set up the worksheets
Set sourceSheet = ActiveSheet
Set targetSheet = Worksheets.Add(After:=Worksheets(Worksheets.Count))

' Find the last row as column on the source sheet
lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, 1).End(xlUp).Row
lastCol = sourceSheet.Cells(1, sourceSheet.Columns.Count).End(xlToLeft).Column

' Set the headers on the target sheet
targetSheet.Cells(1, 1).Value = "Comparative Name"
targetSheet.Cells(1, 2).Value = "Comparative Part No."
targetSheet.Cells(1, 3).Value = "Your Part No."

' The next row on the target sheet
nextRow = 2

' Process all columns
For thisCol = 2 To lastCol
    ' Process all rows
    For thisRow = 2 To lastCol
        ' Split up the values in the current cell
        allValues = Split(sourceSheet.Cells(thisRow, thisCol).Value, ",")
        
        ' Process all values
        For thisValue = 0 To UBound(allValues)
            ' Make sure it's not a blank value
            If allValues(thisValue) <> "" Then
                ' Populate the data in this row
                targetSheet.Cells(nextRow, 1).Value = sourceSheet.Cells(1, thisCol).Value
                targetSheet.Cells(nextRow, 2).Value = allValues(thisValue)
                targetSheet.Cells(nextRow, 3).Value = sourceSheet.Cells(thisRow, 1).Value
                
                ' Move to the next row
                nextRow = nextRow + 1
            End If
        Next thisValue
    Next thisRow
Next thisCol

End Sub

WBD
 
Upvote 0
Thanks WBD, it seems to work, almost! If you look at the results below it is missing the leading and ending zero on some numbers which I have highlighted. Thanks


Excel 2010
ABC
1Comparative NameComparative Part No.Your Part No.
2ALT5_CODE22624EXDN6031
3ALT5_CODE1236EXCN2013
4ALT5_CODE1019EXCN2013
5ALT5_CODE1011EXCN6039
6ALT5_CODE1145EXCN6039
7ALT5_CODE1033EXCN6039
8ALT5_CODE1010EXCN3035
9ALT10_CODENIS6032EXDN6031
10ALT10_CODE38634EXDN6031
11ALT10_CODECIT2014EXCN2013
12ALT10_CODECIT6040EXCN6039
13ALT10_CODECIT3036EXCN3035
14ALT11_CODEP4261EXDN6031
15ALT11_CODENS53105EXDN6031
16ALT11_CODE1.9217EXDN6031
17ALT12_CODE51.24.07EXDN6031
18ALT12_CODE32.7004EXDN6031
19ALT12_CODE44.002.83EXDN6031
Sheet4
 
Upvote 0
I have also noticed that when I try it on a larger dataset it only transposes the data I posted here as an example?
 
Last edited:
Upvote 0
First issue is down to the number format of the cell. Second issue is an oopsie :-O

Code:
Public Sub TransposeData()

Dim lastRow As Long
Dim lastCol As Long
Dim thisRow As Long
Dim thisCol As Long
Dim nextRow As Long
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim allValues As Variant
Dim thisValue As Variant

' Set up the worksheets
Set sourceSheet = ActiveSheet
Set targetSheet = Worksheets.Add(After:=Worksheets(Worksheets.Count))

' Find the last row as column on the source sheet
lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, 1).End(xlUp).Row
lastCol = sourceSheet.Cells(1, sourceSheet.Columns.Count).End(xlToLeft).Column

' Set the headers on the target sheet
targetSheet.Cells(1, 1).Value = "Comparative Name"
targetSheet.Cells(1, 2).Value = "Comparative Part No."
targetSheet.Cells(1, 3).Value = "Your Part No."

' The next row on the target sheet
nextRow = 2

' Process all columns
For thisCol = 2 To lastCol
    ' Process all rows
    For thisRow = 2 To lastRow
        ' Split up the values in the current cell
        allValues = Split(sourceSheet.Cells(thisRow, thisCol).Value, ",")
        
        ' Process all values
        For thisValue = 0 To UBound(allValues)
            ' Make sure it's not a blank value
            If allValues(thisValue) <> "" Then
                ' Populate the data in this row
                targetSheet.Cells(nextRow, 1).Value = sourceSheet.Cells(1, thisCol).Value
                targetSheet.Cells(nextRow, 2).NumberFormat = "@"
                targetSheet.Cells(nextRow, 2).Value = allValues(thisValue)
                targetSheet.Cells(nextRow, 3).Value = sourceSheet.Cells(thisRow, 1).Value
                
                ' Move to the next row
                nextRow = nextRow + 1
            End If
        Next thisValue
    Next thisRow
Next thisCol

End Sub

WBD
 
Upvote 0
Thanks a lot WBD, works great and thanks for your time.
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,126
Members
452,381
Latest member
Nova88

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