Converting data Wide to long with dates

joseulloa22

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

ITEM_NAME 7/30/2018
8/27/201810/1/201810/29/2018
1690ZZ130162162162
1692ZK727017170
<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> </tbody>
........ ..... ..... ....... .....



into

1690ZZ7/30/2018130
1690ZZ8/27/2018162
1690ZZ10/1/2018162
1690ZZ10/29/2018162
1692ZK7/30/201872
1692ZK8/27/201870
1692ZK10/1/2018171
1692ZK10/29/201870
<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> </tbody>

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

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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

A
B
C
D
E
F
G
H
1
ITEM_NAME
ORGANIZATION_CODE
FORECAST_DESIGNATOR
FORECAST_DATE
FORECAST_END_DATE
QUANTITY
CONFIDENCE_PERCENTAGE
BUCKET_TYPE
2
1690ZZ
30-Jul-2018
130
3
1690ZZ
27-Aug-2018
162
4
1690ZZ
1-Oct-2018
162
5
1690ZZ
29-Oct-2018
162
6
1690ZZ
26-Nov-2018
162
1690ZZ
31-Dec-2018
162

<tbody>
</tbody>
 
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,214,974
Messages
6,122,536
Members
449,088
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