Unpivoting columns in Excel

vinmam123VBA

New Member
Joined
Dec 18, 2020
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Can you please help me on how to unpivot columns in excel? I am aware of power query method however I need VBA code to do the things for me

Below is the requirements:

In raw data we get leave details like below:

Employee NameLeave Date
Colleague 129/10/2020,30/10/2020
Colleague 221/12/2020,22/12/2020,23/12/2020,24/12/2020

We need to change it to below:

Employee NameLeave Date
Colleague 129/10/2020
Colleague 130/10/2020
Colleague 221/12/2020
Colleague 222/12/2020
Colleague 223/12/2020
Colleague 224/12/2020

Regards,
Vinayak
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
This Macro Unpivot Column A:B from A2:B2 to Last to Column F:G.
VBA Code:
Sub UnpivotData()
Dim i As Long
Dim j As Long
Dim k As Long
Dim Lr As Long
Dim Lr2 As Long
Dim cell As Range
Dim MyRange As Range

Lr = Cells(Rows.Count, 1).End(xlUp).Row
Set MyRange = Range("B2:B" & Lr)
For Each cell In MyRange
k = Len(cell) - Len(Replace(cell, ",", ""))
For j = 0 To k
Lr2 = Cells(Rows.Count, 6).End(xlUp).Row
Range("F" & Lr2 + WorksheetFunction.CountA("B2:B" & cell.Row)) = cell.Offset(0, -1)
Range("G" & Lr2 + WorksheetFunction.CountA("B2:B" & cell.Row)) = Mid(cell, 1 + 11 * j, 10)
Debug.Print Lr2 + WorksheetFunction.CountA("B2:B" & cell.Row)
Next j
Next cell
End Sub
 
Upvote 0
This Macro Unpivot Column A:B from A2:B2 to Last to Column F:G.
VBA Code:
Sub UnpivotData()
Dim i As Long
Dim j As Long
Dim k As Long
Dim Lr As Long
Dim Lr2 As Long
Dim cell As Range
Dim MyRange As Range

Lr = Cells(Rows.Count, 1).End(xlUp).Row
Set MyRange = Range("B2:B" & Lr)
For Each cell In MyRange
k = Len(cell) - Len(Replace(cell, ",", ""))
For j = 0 To k
Lr2 = Cells(Rows.Count, 6).End(xlUp).Row
Range("F" & Lr2 + WorksheetFunction.CountA("B2:B" & cell.Row)) = cell.Offset(0, -1)
Range("G" & Lr2 + WorksheetFunction.CountA("B2:B" & cell.Row)) = Mid(cell, 1 + 11 * j, 10)
Debug.Print Lr2 + WorksheetFunction.CountA("B2:B" & cell.Row)
Next j
Next cell
End Sub
Thank you Maabadi for the help!!
 
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