How to transpose the data vertically

akhendre88

Board Regular
Joined
Sep 17, 2015
Messages
76
Hello All,

Can we transpose the data vertically. I have some data in below format.
Emp Id
Access Id
Roles
A001
abcd
READ
WRITE
DELETE

<tbody>
</tbody>




So is there any way so that I align the data in 'Roles' column like
Emp Id
Access Id
Roles
A001
abcd
READ
WRITE
DELETE

<tbody>
</tbody>







I was trying this,
Code:
sht.Range("C" & i) = WorksheetFunction.Transpose(.Range("C" & i))sht.Range("C" & i) = WorksheetFunction.Transpose(.Range("C" & i))
but the output was only first value i.e. READ.

Please suggest.

Thank you :)
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Can you give us a SLIGHTLY LARGER example to work with ???
 
Upvote 0
Hi Michael,
Here is he some data,

Emp IdAccess IdRoles
A001AB01READWRITEDELETEADMINAUDITOR
A002AB02ADMINAUDITOR
A003AB03READWRITEDELETE
A004AB04READDELETEADMINAUDITOR
A005AB05READWRITEAUDITOR
A006AB06READWRITEDELETEADMINAUDITOR
A007AB07NO ACCESS
NO ACCESS
NO ACCESS
NO ACCESS
A008
AB08READWRITEDELETEADMINAUDITOR
A009AB09READWRITEDELETEAUDITOR
A010AB10READWRITEDELETEADMIN

<tbody>
</tbody>
 
Last edited:
Upvote 0
Try this:
Code:
Option Explicit


Sub trnsps()
    Dim s1 As Worksheet, s2 As Worksheet
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    Dim r1 As Long, r2 As Long
    Dim i As Long, lc As Long


    Application.ScreenUpdating = False
    s1.Range("A1:C1").Copy s2.Range("A1")
    r1 = s1.Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To r1
        r2 = s2.Range("C" & Rows.Count).End(xlUp).Row
        s1.Range("A" & i & ":B" & i).Copy s2.Range("A" & r2 + 1)
        lc = Cells(i, Columns.Count).End(xlToLeft).Column
        s1.Range(Cells(i, 3), Cells(i, lc)).Copy
        s2.Range("C" & r2 + 1).PasteSpecial xlPasteAll, , , True
        Application.CutCopyMode = False
    Next i
    Application.ScreenUpdating = True
    MsgBox "complete"
End Sub
 
Upvote 0
Depending on how large your data is, and just where you want the results, you might also want to test this in a copy of your workbook.

Rich (BB code):
Sub Rearrange()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, ub1 As Long, ub2 As Long
  
  a = Range("A1").CurrentRegion.Value
  ub1 = UBound(a, 1)
  ub2 = UBound(a, 2)
  ReDim b(1 To ub1 * (ub2 - 2), 1 To 3)
  For i = 1 To ub1
    k = k + 1
    b(k, 1) = a(i, 1): b(k, 2) = a(i, 2): b(k, 3) = a(i, 3)
    For j = 4 To ub2
      If IsEmpty(a(i, j)) Then
        Exit For
      Else
        k = k + 1
        b(k, 3) = a(i, j)
      End If
    Next j
  Next i
  Range("A1").Offset(, UBound(a, 2) + 2).Resize(k, 3).Value = b
End Sub


For original data in columns A:G, the code above produced the results seen in columns J:L

Excel Workbook
ABCDEFGHIJKL
1Emp IdAccess IdRolesEmp IdAccess IdRoles
2A001AB01READWRITEDELETEADMINAUDITORA001AB01READ
3A002AB02ADMINAUDITORWRITE
4A003AB03READWRITEDELETEDELETE
5A004AB04READDELETEADMINAUDITORADMIN
6A005AB05READWRITEAUDITORAUDITOR
7A006AB06READWRITEDELETEADMINAUDITORA002AB02ADMIN
8A007AB07NO ACCESSNO ACCESSNO ACCESSNO ACCESSAUDITOR
9A008AB08READWRITEDELETEADMINAUDITORA003AB03READ
10A009AB09READWRITEDELETEAUDITORWRITE
11A010AB10READWRITEDELETEADMINDELETE
12A004AB04READ
13DELETE
14ADMIN
15AUDITOR
16A005AB05READ
17WRITE
18AUDITOR
19A006AB06READ
20WRITE
21DELETE
22ADMIN
23AUDITOR
24A007AB07NO ACCESS
25NO ACCESS
26NO ACCESS
27NO ACCESS
28A008AB08READ
29WRITE
30DELETE
31ADMIN
32AUDITOR
33A009AB09READ
34WRITE
35DELETE
36AUDITOR
37A010AB10READ
38WRITE
39DELETE
40ADMIN
Rearrange
 
Upvote 0
Hi Alan,

Thank you very much for your help. However,
Code:
s1.Range(Cells(i, 3), Cells(i, lc)).Copy
this line is giving me an error "Method Range of object _Worksheet failed".

I tried from my side as well to resolve this but not able to do much with this
 
Upvote 0
unable to reproduce your error. The code works for me. My results after running the code
Emp IdAccess IdRoles
A001AB01READ
WRITE
DELETE
ADMIN
AUDITOR
A002AB02ADMIN
AUDITOR
A003AB03READ
WRITE
DELETE
A004AB04READ
DELETE
ADMIN
AUDITOR
A005AB05READ
WRITE
AUDITOR
A006AB06READ
WRITE
DELETE
ADMIN
AUDITOR
A007AB07NO ACCESS
NO ACCESS
NO ACCESS
NO ACCESS
A008AB08READ
WRITE
DELETE
ADMIN
AUDITOR
A009AB09READ
WRITE
DELETE
AUDITOR
A010AB10READ
WRITE
DELETE
ADMIN

<colgroup><col span="4"></colgroup><tbody>
</tbody>
 
Upvote 0
Excellent Point Peter. I have changed up the code as follows:

Code:
Option Explicit


Sub trnsps()
    Dim s1 As Worksheet, s2 As Worksheet
    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")
    Dim r1 As Long, r2 As Long
    Dim i As Long, lc As Long


    Application.ScreenUpdating = False
    
    With s1
        .Activate
        .Range("A1:C1").Copy s2.Range("A1")
        r1 = .Range("A" & Rows.Count).End(xlUp).Row
        For i = 2 To r1
            r2 = s2.Range("C" & Rows.Count).End(xlUp).Row
            .Range("A" & i & ":B" & i).Copy s2.Range("A" & r2 + 1)
            lc = Cells(i, Columns.Count).End(xlToLeft).Column
            .Range(Cells(i, 3), Cells(i, lc)).Copy
            s2.Range("C" & r2 + 1).PasteSpecial xlPasteAll, , , True
            Application.CutCopyMode = False
        Next i
    End With
    Application.ScreenUpdating = True
    MsgBox "complete"
End Sub

@akhendre88
To Peter's point. Have you tested his code? There is usually more than one way to solve an excel issue and this is a good example.
 
Upvote 0

Forum statistics

Threads
1,215,575
Messages
6,125,629
Members
449,241
Latest member
NoniJ

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