VBA Macro to transpose 4 columns into two columns

nutbolt

New Member
Joined
Apr 2, 2021
Messages
17
Office Version
  1. 2019
Hi I've been looking at a lot of transpose demos, still can't get what I need to do this so let me explain. I have a four column table and I am looking to Transpose it over to a two column table or array?
It will reuse the column headers so I'm thinking a loop for them and a counter to increment, I'm confused if it can be done in Array or transpose. My image is below to help explain it. I'm looking for the table to become the two columns with data like that under it.
 

Attachments

  • Screenshot 2022-12-26 171745.jpg
    Screenshot 2022-12-26 171745.jpg
    115 KB · Views: 37

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Is this what you want?

VBA Code:
Sub Rearrange()
  Dim a As Variant, Hdr As Variant
  Dim i As Long, r As Long
  
  a = Range("A1", Range("D" & Rows.Count).End(xlUp)).Value
  Hdr = Application.Transpose(Application.Index(a, 1, 0))
  r = Range("A" & Rows.Count).End(xlUp).Row - 2
  For i = 2 To UBound(a)
    r = r + 5
    Range("A" & r).Resize(4).Value = Hdr
    Range("B" & r).Resize(4).Value = Application.Transpose(Application.Index(a, i, 0))
  Next i
End Sub
 
Upvote 0
If you add this before the "Next i" in Peter's code it will restore the hyperlink for the email address.
VBA Code:
    Range("B" & r).Hyperlinks.Add Anchor:=Range("B" & r), Address:="mailto:" & Range("B" & r).Value
 
Upvote 0
restore the hyperlink for the email address.
Good point.

Another way to retain those would be ..

VBA Code:
Sub Rearrange_v2()
  Dim r As Range
  Dim i As Long
  
  Set r = Range("A1", Range("D" & Rows.Count).End(xlUp))
  For i = 2 To r.Rows.Count
    Union(r.Rows(1), r.Rows(i)).Copy
    Range("A" & Rows.Count).End(xlUp).Offset(2).PasteSpecial Paste:=xlPasteAll, Transpose:=True
  Next i
  Application.CutCopyMode = False
End Sub
 
Upvote 0
Solution
Good point.

Another way to retain those would be ..

VBA Code:
Sub Rearrange_v2()
  Dim r As Range
  Dim i As Long
 
  Set r = Range("A1", Range("D" & Rows.Count).End(xlUp))
  For i = 2 To r.Rows.Count
    Union(r.Rows(1), r.Rows(i)).Copy
    Range("A" & Rows.Count).End(xlUp).Offset(2).PasteSpecial Paste:=xlPasteAll, Transpose:=True
  Next i
  Application.CutCopyMode = False
End Sub
works great thanks
 
Upvote 0
You're welcome. Thanks for the follow-up.
 
Upvote 0

Forum statistics

Threads
1,214,967
Messages
6,122,503
Members
449,090
Latest member
RandomExceller01

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