Transpose Column to Rows

hardeep.kanwar

Well-known Member
Joined
Aug 13, 2008
Messages
693
Hi! Experts

I have data in Column A and Column B

See the Example

Now, I want to Transpose the Column B into another Sheet

Excel Workbook
AB
1RecordDetails
2Nameamruta jagtap42986
3Emailamrutaj100@rediffmail.com
4Phone8055523115
5GenderFemale
6Desired CourseMBA/PGDM
7ModeFull Time
8Current LocationAll Mumbai
9Preferred LocationsAll Mumbai
10Date of Registration14th Sep 2011
11
12NameKRISHNA59028
13EmailKRISHNAGOJE11@GMAIL.COM
14Phone9533866967
15Desired CourseMBA/PGDM
16ModeFull Time
17Current LocationAndhra Pradesh - Other
18Preferred LocationsHyderabad
19Date of Registration14th Sep 2011
20
21Namepankaj gadgul96032
22Emailpgadgul@gmail.com
23Phone9945079470
24GenderMale
25Desired CourseMBA/PGDM
26ModePart Time
27Current LocationBangalore
28Preferred LocationsPune,Bangalore,Hyderabad
29
30NameHazra79419
31Emailhazrakhatoon@yahoo.com
32Phone9007324142
33GenderFemale
34Desired CourseDiploma in Fashion Design
35Current LocationKolkata
36Preferred LocationsKolkata
37Date of Registration14th Sep 2011
38
39NameNidhi Khanna41911
40Emailrachalkhanna@ymail.com
41Phone9212114490
42GenderFemale
43Desired CourseDiploma in Fashion Design
44ModePart Time
45Current LocationDelhi
46Date of Registration14th Sep 2011
Sheet2


This is the Format in Sheet 2 Row 1


Excel Workbook
EFGHIJKLMN
1NameEmailPhoneGenderDesired CourseModeCurrent LocationPreferred LocationsDate of Registration
Sheet1


Expected Result

<table style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; padding-left:2pt; padding-right:2pt; " border="1" cellpadding="0" cellspacing="0"><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td>
</td><td>
</td><td>
</td><td>
</td><td>
</td><td>
</td><td>
</td><td>
</td><td>
</td><td>
</td></tr><tr style="height:29px ;"><td style="font-size:8pt; background-color:#cacaca; text-align:center; ">
</td><td style="font-weight:bold; font-family:Arial; font-size:9pt; text-align:center; ">
</td><td style="color:#0000ff; text-decoration:underline; text-align:center; ">
</td><td style="font-weight:bold; font-family:Arial; font-size:9pt; text-align:center; ">
</td><td style="font-weight:bold; font-family:Arial; font-size:9pt; text-align:center; ">
</td><td style="font-weight:bold; font-family:Arial; font-size:9pt; text-align:center; ">
</td><td style="font-weight:bold; font-family:Arial; font-size:9pt; text-align:center; ">
</td><td style="font-weight:bold; font-family:Arial; font-size:9pt; text-align:center; ">
</td><td style="font-weight:bold; font-family:Arial; font-size:9pt; text-align:center; ">
</td><td style="font-weight:bold; font-family:Arial; font-size:9pt; text-align:center; ">
</td></tr><tr style="height:18px ;"><td style="font-size:8pt; background-color:#cacaca; text-align:center; ">
</td><td>
</td><td>
</td><td style="text-align:right; ">
</td><td>
</td><td>
</td><td>
</td><td>
</td><td>
</td><td>
</td></tr></table>
But, the Problem is the Format in not Same in Column A in Sheet1, See the Example No. 2 & 3 , there are only 8 Fields, 1 Field is Missing

Excel Workbook
DEFGHIJKL
1NameEmailPhoneGenderDesired CourseModeCurrent LocationPreferred LocationsDate of Registration
2amruta jagtap42986amrutaj100@rediffmail.com8055523115FemaleMBA/PGDMFull TimeAll MumbaiAll Mumbai14th Sep 2011
3KRISHNA59028KRISHNAGOJE11@GMAIL.COM9533866967MBA/PGDMFull TimeAndhra Pradesh - OtherHyderabad14th Sep 2011
4pankaj gadgul96032pgadgul@gmail.com9945079470MaleMBA/PGDMPart TimeBangalorePune,Bangalore,Hyderabad
5Hazra79419hazrakhatoon@yahoo.com9007324142FemaleDiploma in Fashion DesignKolkataKolkata14th Sep 2011
Sheet2


See the Highlited Rows



 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try...
Code:
Sub trans()
    
    Set sh = Sheets(2)
    sh.Cells.ClearContents
    Range("A2:A9").Copy
    sh.Range("A1").PasteSpecial Paste:=xlPasteAll, Transpose:=True
    lr = sh.Cells(Rows.Count).End(xlUp).Row
For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row Step 9
    Set A_Rng = Range("A" & i)
    Set B_Rng = Range("B" & i)
    Set B2_Rng = sh.Range("A" & lr + 1)
    
    Range("B" & i & ":B" & i + 7).Copy
    B2_Rng.PasteSpecial Paste:=xlPasteAll, Transpose:=True
    lr = lr + 1
Next
End Sub
Assuming that column A have the same fields as headers
 
Upvote 0
Try...
Code:
Sub trans()
    
    Set sh = Sheets(2)
    sh.Cells.ClearContents
    Range("A2:A9").Copy
    sh.Range("A1").PasteSpecial Paste:=xlPasteAll, Transpose:=True
    lr = sh.Cells(Rows.Count).End(xlUp).Row
For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row Step 9
    Set A_Rng = Range("A" & i)
    Set B_Rng = Range("B" & i)
    Set B2_Rng = sh.Range("A" & lr + 1)
    
    Range("B" & i & ":B" & i + 7).Copy
    B2_Rng.PasteSpecial Paste:=xlPasteAll, Transpose:=True
    lr = lr + 1
Next
End Sub
Assuming that column A have the same fields as headers

Sorry for Delay Reply


The Main Problem is the Header are not Same

See the Example no.2 and 3, i.e. A12:A19 and A21:A28
 
Upvote 0
Try this, it will find the correct spot for any header location:
Code:
Option Explicit

Sub ColumnsToRows()
Dim LR As Long, NR As Long, Rw As Long
Dim wsData As Worksheet, wsOUT As Worksheet
Dim HdrCol As Range, Hdr As String

Set wsData = Sheets("Sheet2")
Set wsOUT = Sheets("Sheet1")
LR = wsData.Range("A" & Rows.Count).End(xlUp).Row
NR = wsOUT.Range("E" & Rows.Count).End(xlUp).Row

On Error Resume Next
For Rw = 1 To LR
    Hdr = wsData.Range("A" & Rw).Value
    If Hdr = "Name" Then NR = NR + 1
    If Hdr <> "" And Hdr <> "Record" Then
        Set HdrCol = wsOUT.Range("1:1").Find(Hdr, _
                LookIn:=xlValues, LookAt:=xlWhole)
        If Not HdrCol Is Nothing Then
            wsOUT.Cells(NR, HdrCol.Column).Value = wsData.Range("B" & Rw).Value
            Set HdrCol = Nothing
        End If
    End If
Next Rw

wsOUT.Columns.AutoFit
End Sub
 
Last edited:
Upvote 0
This slight tweak also allows you to set the "string" at the top of the macro that will "reset" each time and create a new output row. This means nothing on the OUTPUT sheet is hardcoded, it's all variable and self-correcting.
Rich (BB code):
Option Explicit

Sub ColumnsToRows()
Dim LR As Long, NR As Long, Rw As Long
Dim wsData As Worksheet, wsOUT As Worksheet
Dim HdrCol As Range, Hdr As String, strRESET As String

Set wsData = Sheets("Sheet2")   'source data
Set wsOUT = Sheets("Sheet1")    'output sheet
strRESET = "Name"               'this value will cause the record row to increment
LR = wsData.Range("A" & Rows.Count).End(xlUp).Row   'end of incoming data

Set HdrCol = wsOUT.Range("1:1") _
    .Find(strRESET, LookIn:=xlValues, LookAt:=xlWhole)
If HdrCol Is Nothing Then
    MsgBox "The key string '" & strRESET & "' could not be found on the output sheet."
    Exit Sub
End If

NR = wsOUT.Cells(Rows.Count, HdrCol.Column).End(xlUp).Row   'current output end of data
Set HdrCol = Nothing

On Error Resume Next
For Rw = 1 To LR
    Hdr = wsData.Range("A" & Rw).Value
    If Hdr = "Name" Then NR = NR + 1
    If Hdr <> "" And Hdr <> "Record" Then
        Set HdrCol = wsOUT.Range("1:1").Find(Hdr, _
                LookIn:=xlValues, LookAt:=xlWhole)
        If Not HdrCol Is Nothing Then
            wsOUT.Cells(NR, HdrCol.Column).Value = wsData.Range("B" & Rw).Value
            Set HdrCol = Nothing
        End If
    End If
Next Rw

wsOUT.Columns.AutoFit
End Sub
 
Upvote 0
This slight tweak also allows you to set the "string" at the top of the macro that will "reset" each time and create a new output row. This means nothing on the OUTPUT sheet is hardcoded, it's all variable and self-correcting.
Rich (BB code):
Option Explicit

Sub ColumnsToRows()
Dim LR As Long, NR As Long, Rw As Long
Dim wsData As Worksheet, wsOUT As Worksheet
Dim HdrCol As Range, Hdr As String, strRESET As String

Set wsData = Sheets("Sheet2")   'source data
Set wsOUT = Sheets("Sheet1")    'output sheet
strRESET = "Name"               'this value will cause the record row to increment
LR = wsData.Range("A" & Rows.Count).End(xlUp).Row   'end of incoming data

Set HdrCol = wsOUT.Range("1:1") _
    .Find(strRESET, LookIn:=xlValues, LookAt:=xlWhole)
If HdrCol Is Nothing Then
    MsgBox "The key string '" & strRESET & "' could not be found on the output sheet."
    Exit Sub
End If

NR = wsOUT.Cells(Rows.Count, HdrCol.Column).End(xlUp).Row   'current output end of data
Set HdrCol = Nothing

On Error Resume Next
For Rw = 1 To LR
    Hdr = wsData.Range("A" & Rw).Value
    If Hdr = "Name" Then NR = NR + 1
    If Hdr <> "" And Hdr <> "Record" Then
        Set HdrCol = wsOUT.Range("1:1").Find(Hdr, _
                LookIn:=xlValues, LookAt:=xlWhole)
        If Not HdrCol Is Nothing Then
            wsOUT.Cells(NR, HdrCol.Column).Value = wsData.Range("B" & Rw).Value
            Set HdrCol = Nothing
        End If
    End If
Next Rw

wsOUT.Columns.AutoFit
End Sub

Sir You are a Genius, You are Really Life Save for Me:)

Thank you Very Much Sir
:pray::beerchug:
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,220
Members
452,895
Latest member
BILLING GUY

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