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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Can you please post some sample data using the XL2BB add-in
Will every location always have all 6 options?
 
Upvote 0
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


View attachment 11315View attachment 11316
Can you please post some sample data using the XL2BB add-in
Will every location always have all 6 options?

Can you please post some sample data using the XL2BB add-in
Will every location always have all 6 options?
Here it is
Dummy Data.xlsx
ABCDEFGHIJKLMN
1Current DatasetDesired Outome
2DrugPeriodLocationOptionsValuesDrugPeriodLocationCollectedSoldExpiredDamagedAdministeredNeeded
3Vitamin C201909HospitalCollected3Vitamin C201909Hospital3210410
4Vitamin C201909HospitalSold2Vitamin C201909Facility23451012
5Vitamin C201909HospitalExpired1Vitamin D201909Health clinic10159876
6Vitamin C201909HospitalDamaged0Vitamin D201909Health Post1214118921
7Vitamin C201909HospitalAdministered4
8Vitamin C201909HospitalNeeded10
9Vitamin C201909facilityCollected2
10Vitamin C201909facilitySold3
11Vitamin C201909facilityExpired4
12Vitamin C201909facilityDamaged5
13Vitamin C201909facilityAdministered10
14Vitamin C201909facilityNeeded12
15Vitamin D201909Health clinicCollected10
16Vitamin D201909Health clinicSold15
17Vitamin D201909Health clinicExpired9
18Vitamin D201909Health clinicDamaged8
19Vitamin D201909Health clinicAdministered7
20Vitamin D201909Health clinicNeeded6
21Vitamin D201909Health PostCollected12
22Vitamin D201909Health PostSold14
23Vitamin D201909Health PostExpired11
24Vitamin D201909Health PostDamaged8
25Vitamin D201909Health PostAdministered9
26Vitamin D201909Health PostNeeded21
Currently
 
Upvote 0
Thanks for that, could you also please answer my question?
 
Upvote 0
Ok, how about
VBA Code:
Sub Bisola()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long
   
   Ary = Range("A2: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
Ok, how about
VBA Code:
Sub Bisola()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long
  
   Ary = Range("A2: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
Okay, I'll try this out
 
Upvote 0
Ok, how about
VBA Code:
Sub Bisola()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, c As Long, nr As Long
  
   Ary = Range("A2: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
Thanks but when I run it, It pops an error (Run-time error '9' Subscript out of range)
 
Upvote 0
Which line gives that error?
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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