Converting data Wide to long with dates

joseulloa22

New Member
Joined
Dec 28, 2013
Messages
30
Hello,
I was trying to convert the following table

[TABLE="width: 293"]
<colgroup><col width="94" style="width: 71pt; mso-width-source: userset; mso-width-alt: 3437;"> <col width="85" style="width: 64pt; mso-width-source: userset; mso-width-alt: 3108;"> <col width="68" style="width: 51pt; mso-width-source: userset; mso-width-alt: 2486;" span="2"> <col width="75" style="width: 56pt; mso-width-source: userset; mso-width-alt: 2742;"> <tbody>[TR]
[TD="width: 94, bgcolor: transparent"]ITEM_NAME[/TD]
[TD="width: 85, bgcolor: transparent, align: right"] 7/30/2018
[/TD]
[TD="width: 68, bgcolor: transparent, align: right"]8/27/2018[/TD]
[TD="width: 68, bgcolor: transparent, align: right"]10/1/2018[/TD]
[TD="width: 75, bgcolor: transparent, align: right"]10/29/2018
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]1690ZZ[/TD]
[TD="bgcolor: transparent, align: right"]130[/TD]
[TD="bgcolor: transparent, align: right"]162[/TD]
[TD="bgcolor: transparent, align: right"]162[/TD]
[TD="bgcolor: transparent, align: right"]162[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]1692ZK[/TD]
[TD="bgcolor: transparent, align: right"]72[/TD]
[TD="bgcolor: transparent, align: right"]70[/TD]
[TD="bgcolor: transparent, align: right"]171[/TD]
[TD="bgcolor: transparent, align: right"]70[/TD]
[/TR]
</tbody>[/TABLE]
........ ..... ..... ....... .....



into

[TABLE="width: 186"]
<colgroup><col width="94" style="width: 71pt; mso-width-source: userset; mso-width-alt: 3437;"> <col width="85" style="width: 64pt; mso-width-source: userset; mso-width-alt: 3108;"> <col width="68" style="width: 51pt; mso-width-source: userset; mso-width-alt: 2486;"> <tbody>[TR]
[TD="width: 94, bgcolor: transparent"]1690ZZ[/TD]
[TD="width: 85, bgcolor: transparent, align: right"]7/30/2018[/TD]
[TD="width: 68, bgcolor: transparent, align: right"]130[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]1690ZZ[/TD]
[TD="bgcolor: transparent, align: right"]8/27/2018[/TD]
[TD="bgcolor: transparent, align: right"]162[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]1690ZZ[/TD]
[TD="bgcolor: transparent, align: right"]10/1/2018[/TD]
[TD="bgcolor: transparent, align: right"]162[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]1690ZZ[/TD]
[TD="bgcolor: transparent, align: right"]10/29/2018[/TD]
[TD="bgcolor: transparent, align: right"]162[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]1692ZK[/TD]
[TD="bgcolor: transparent, align: right"]7/30/2018[/TD]
[TD="bgcolor: transparent, align: right"]72[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]1692ZK[/TD]
[TD="bgcolor: transparent, align: right"]8/27/2018[/TD]
[TD="bgcolor: transparent, align: right"]70[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]1692ZK[/TD]
[TD="bgcolor: transparent, align: right"]10/1/2018[/TD]
[TD="bgcolor: transparent, align: right"]171[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]1692ZK[/TD]
[TD="bgcolor: transparent, align: right"]10/29/2018[/TD]
[TD="bgcolor: transparent, align: right"]70[/TD]
[/TR]
</tbody>[/TABLE]

This is an example but my table has more data. Your wisdom is greatly appreciated
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Assuming the table starts at A1, give this a try in a copy of your workbook.
Code:
Sub Rearrange()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, ub2 As Long
  
  With Range("A1").CurrentRegion
    a = .Value
    ub2 = UBound(a, 2)
    ReDim b(1 To (UBound(a, 1) - 1) * (ub2 - 1), 1 To 3)
    For i = 2 To UBound(a)
      For j = 2 To ub2
        k = k + 1
        b(k, 1) = a(i, 1): b(k, 2) = a(1, j): b(k, 3) = a(i, j)
      Next j
    Next i
    .Offset(.Rows.Count + 3).Resize(k, 3).Value = b
  End With
End Sub
 
Upvote 0
Hi Peter
This is great. It worked and worked fantastic. Question, is there any way to make the new table comes up in a new sheet, say starting in cell A1?

Thank you again!
Jose Ulloa
 
Upvote 0
.. is there any way to make the new table comes up in a new sheet, say starting in cell A1?
Try something like this
Code:
Sub Rearrange_v2()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long, ub2 As Long
  
  a = Range("A1").CurrentRegion.Value
  ub2 = UBound(a, 2)
  ReDim b(1 To (UBound(a, 1) - 1) * (ub2 - 1), 1 To 3)
  For i = 2 To UBound(a)
    For j = 2 To ub2
      k = k + 1
      b(k, 1) = a(i, 1): b(k, 2) = a(1, j): b(k, 3) = a(i, j)
    Next j
  Next i
  Sheets.Add After:=ActiveSheet
  With Range("A1:C1")
    .Value = Array("Item", "Date", "Value")
    .Rows(2).Resize(k).Value = b
    .EntireColumn.AutoFit
  End With
End Sub
 
Upvote 0
Peter, sorry to bother you again. Can you make the output look like the following? Change/Added new columns with headers name

[TABLE="width: 784"]
<tbody>[TR]
[TD="width: 63, bgcolor: transparent"][/TD]
[TD="width: 82, bgcolor: transparent"]A
[/TD]
[TD="width: 147, bgcolor: transparent"]B
[/TD]
[TD="width: 160, bgcolor: transparent"]C
[/TD]
[TD="width: 110, bgcolor: transparent"]D
[/TD]
[TD="width: 144, bgcolor: transparent"]E
[/TD]
[TD="width: 70, bgcolor: transparent"]F
[/TD]
[TD="width: 177, bgcolor: transparent"]G
[/TD]
[TD="width: 90, bgcolor: transparent"]H
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]1
[/TD]
[TD="bgcolor: transparent"]ITEM_NAME
[/TD]
[TD="bgcolor: transparent"]ORGANIZATION_CODE
[/TD]
[TD="bgcolor: transparent"]FORECAST_DESIGNATOR
[/TD]
[TD="bgcolor: transparent"]FORECAST_DATE
[/TD]
[TD="bgcolor: transparent"]FORECAST_END_DATE
[/TD]
[TD="bgcolor: transparent"]QUANTITY
[/TD]
[TD="bgcolor: transparent"]CONFIDENCE_PERCENTAGE
[/TD]
[TD="bgcolor: transparent"]BUCKET_TYPE
[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]2
[/TD]
[TD="bgcolor: transparent"]1690ZZ
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]30-Jul-2018
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]130
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]3
[/TD]
[TD="bgcolor: transparent"]1690ZZ
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]27-Aug-2018
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]162
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]4
[/TD]
[TD="bgcolor: transparent"]1690ZZ
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]1-Oct-2018
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]162
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]5
[/TD]
[TD="bgcolor: transparent"]1690ZZ
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]29-Oct-2018
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]162
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"]6
[/TD]
[TD="bgcolor: transparent"]1690ZZ
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]26-Nov-2018
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]162
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
[TR]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"]1690ZZ
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]31-Dec-2018
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent, align: right"]162
[/TD]
[TD="bgcolor: transparent"][/TD]
[TD="bgcolor: transparent"][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Can you make the output look like the following?
Replace the code between the blue rows with the code shown below.
Rich (BB code):
  Next i
  Application.ScreenUpdating = False
  Sheets.Add After:=ActiveSheet
  Range("A2:C2").Resize(k).Value = b
  Columns("C").Insert
  Columns("B:C").Insert
  Range("A1:H1").Value = Split("ITEM_NAME ORGANIZATION_CODE FORECAST_DESIGNATOR FORECAST_DATE FORECAST_END_DATE QUANTITY CONFIDENCE_PERCENTAGE BUCKET_TYPE")
  Columns("A:H").AutoFit
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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