Transpose multiple columns based on unique values

Bisola

New Member
Joined
Apr 15, 2020
Messages
9
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone,
I'm trying to transpose multiple columns based on unique values but I don't know how to go about it. It can't be done manually because the dataset is really large about 300,000 rows. So, I'm going to share a dummy file of how it is currently and how it's expected to be. Please help, it's quite urgent


1586946620065.png
1586946684768.png
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Does your data start in row 2 with a header in row 1?
Also is it laid out exactly as the data you supplied?
 
Upvote 0
Does your data start in row 2 with a header in row 1?
Also is it laid out exactly as the data you supplied?
Yes, the data start in row 2 with a header in row 1 exactly as the dummy dataset I shared, Although Row 1 is merged just as it in the excel file shared
 
Upvote 0
In that case try
VBA Code:
Sub Bisola()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long
   
   Ary = Range("A3:E" & Range("A" & Rows.Count).End(xlUp).Row)
   ReDim Nary(1 To UBound(Ary), 1 To 9)
   For r = 1 To UBound(Ary) Step 6
      nr = nr + 1
      Nary(nr, 1) = Ary(r, 1)
      Nary(nr, 2) = Ary(r, 2)
      Nary(nr, 3) = Ary(r, 3)
      For c = 0 To 5
         Nary(nr, c + 4) = Ary(r + c, 5)
      Next c
   Next r
   Range("H1:P1").Value = Array("Drug", "Period", "Location", "Collected", "Sold", "Expired", "Damaged", "Administered", "Needed")
   Range("H2").Resize(nr, 9).Value = Nary
End Sub
 
Upvote 0
In that case try
VBA Code:
Sub Bisola()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long
  
   Ary = Range("A3:E" & Range("A" & Rows.Count).End(xlUp).Row)
   ReDim Nary(1 To UBound(Ary), 1 To 9)
   For r = 1 To UBound(Ary) Step 6
      nr = nr + 1
      Nary(nr, 1) = Ary(r, 1)
      Nary(nr, 2) = Ary(r, 2)
      Nary(nr, 3) = Ary(r, 3)
      For c = 0 To 5
         Nary(nr, c + 4) = Ary(r + c, 5)
      Next c
   Next r
   Range("H1:P1").Value = Array("Drug", "Period", "Location", "Collected", "Sold", "Expired", "Damaged", "Administered", "Needed")
   Range("H2").Resize(nr, 9).Value = Nary
End Sub
Yay!!!!!!! It worked! Thanks so much
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Pivot Table result

Book1
FGHIJKLMNOP
1Desired Outome
2DrugPeriodLocationCollectedSoldExpiredDamagedAdministeredNeeded
3Vitamin C201909Hospital3210410
4Vitamin C201909Facility23451012
5Vitamin D201909Health clinic10159876
6Vitamin D201909Health Post1214118921
7
8
9
10Sum of Values2Options
11DrugPeriodLocationAdministeredCollectedDamagedExpiredNeededSold
12Vitamin C
13201909
14facility10254123
15Hospital4301102
16Vitamin D
17201909
18Health clinic71089615
19Health Post9128112114
20
Sheet1
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,843
Members
449,051
Latest member
excelquestion515

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