VBA or formula to transpose/re-arranging data in columns

rozek

New Member
Joined
Aug 11, 2021
Messages
29
Office Version
  1. 365
Platform
  1. Windows
Hello. Appreciate any help on this transpose/re-arranging of columns. What I wanted to achieved is straight forward but seems difficult to do.

I wanted to re arrange this dataset below:
Book1
ABCD
1SALARY
2DATENAME1NAME2NAME3
31/1/2021101100
42/1/2021202200
53/1/2021303300
64/1/2021404400
75/1/2021505500
86/1/2021606600
97/1/2021707700
108/1/2021808800
119/1/2021909900
1210/1/2021100101000
Sheet1 (2)


To be in this arrangement
Book1
ABC
1NAMEDATESALARY
2NAME11/1/202110
3NAME12/1/202120
4NAME13/1/202130
5NAME14/1/202140
6NAME15/1/202150
7NAME16/1/202160
8NAME17/1/202170
9NAME18/1/202180
10NAME19/1/202190
11NAME110/1/2021100
12NAME21/1/20211
13NAME22/1/20212
14NAME23/1/20213
15NAME24/1/20214
16NAME25/1/20215
17NAME26/1/20216
18NAME27/1/20217
19NAME28/1/20218
20NAME29/1/20219
21NAME210/1/202110
22NAME31/1/2021100
23NAME32/1/2021200
24NAME33/1/2021300
25NAME34/1/2021400
26NAME35/1/2021500
27NAME36/1/2021600
28NAME37/1/2021700
29NAME38/1/2021800
30NAME39/1/2021900
31NAME310/1/20211000
Sheet1 (2)


Any help is much appreciated. Thank you.
 
Try this then. I assume columns F:H are free to receive the results.

VBA Code:
Sub Rearrange()
  Dim addrA As String, addr2 As String, addrData
  
  With Range("B3", Range("A2").End(xlToRight).End(xlDown))
    addrA = .Columns(0).Address(1, 1, xlR1C1)
    addr2 = .Rows(0).Address(1, 1, xlR1C1)
    addrData = .Address(1, 1, xlR1C1)
  End With
  Range("F2:H2").Value = Array("NAME", "DATE", "SALARY")
  Range("F3").Formula2R1C1 = Replace(Replace(Replace( _
      "=LET(rws,ROWS(#),cols,COLUMNS(%),c_1,INDEX(%,INT(SEQUENCE(rws*cols,,rws)/rws))," _
        & "c_2,INDEX(#,MOD(SEQUENCE(rws*cols,,0),rws)+1),CHOOSE({1,2,3},c_1,c_2," _
        & "INDEX(@,MATCH(c_2,#,0),MATCH(c_1,%,0))))", "#", addrA), "%", addr2), "@", addrData)
  Columns("G").NumberFormat = "d/m/yyyy"
End Sub
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Try this then. I assume columns F:H are free to receive the results.

VBA Code:
Sub Rearrange()
  Dim addrA As String, addr2 As String, addrData
 
  With Range("B3", Range("A2").End(xlToRight).End(xlDown))
    addrA = .Columns(0).Address(1, 1, xlR1C1)
    addr2 = .Rows(0).Address(1, 1, xlR1C1)
    addrData = .Address(1, 1, xlR1C1)
  End With
  Range("F2:H2").Value = Array("NAME", "DATE", "SALARY")
  Range("F3").Formula2R1C1 = Replace(Replace(Replace( _
      "=LET(rws,ROWS(#),cols,COLUMNS(%),c_1,INDEX(%,INT(SEQUENCE(rws*cols,,rws)/rws))," _
        & "c_2,INDEX(#,MOD(SEQUENCE(rws*cols,,0),rws)+1),CHOOSE({1,2,3},c_1,c_2," _
        & "INDEX(@,MATCH(c_2,#,0),MATCH(c_1,%,0))))", "#", addrA), "%", addr2), "@", addrData)
  Columns("G").NumberFormat = "d/m/yyyy"
End Sub
Awesome code Peter. Thank you. If I want to re-arrange it to another sheet, where should i tweak the code at.
 
Upvote 0
If I want to re-arrange it to another sheet, where should i tweak the code at.
Try this. Check/edit sheet names in the code and I have assumed the results sheet does not already contain data in columns A:C.

VBA Code:
Sub Rearrange_v2()
  Dim addrA As String, addr2 As String, addrData
  Dim wsD As Worksheet, wsR As Worksheet
  
  Set wsD = Sheets("Data")
  Set wsR = Sheets("Result")
  
  With wsD.Range("B3", wsD.Range("A2").End(xlToRight).End(xlDown))
    addrA = .Columns(0).Address(1, 1, xlR1C1, True)
    addr2 = .Rows(0).Address(1, 1, xlR1C1, True)
    addrData = .Address(1, 1, xlR1C1, True)
  End With
  With wsR
    .Range("A1:C1").Value = Array("NAME", "DATE", "SALARY")
    .Range("A2").Formula2R1C1 = Replace(Replace(Replace( _
        "=LET(rws,ROWS(#),cols,COLUMNS(%),c_1,INDEX(%,INT(SEQUENCE(rws*cols,,rws)/rws))," _
          & "c_2,INDEX(#,MOD(SEQUENCE(rws*cols,,0),rws)+1),CHOOSE({1,2,3},c_1,c_2," _
          & "INDEX(@,MATCH(c_2,#,0),MATCH(c_1,%,0))))", "#", addrA), "%", addr2), "@", addrData)
    .Columns("B").NumberFormat = "d/m/yyyy"
  End With
End Sub
 
Upvote 0
Solution
Thank you. Awesome. 5 Star rating. Have a good and pleasant day ahead Peter :) (y)
 
Upvote 0
You're welcome. Glad it worked for you. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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