Creating a new row for each date within a row range

sryan429

New Member
Joined
Jul 1, 2016
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi all!
Have a tricky one for you :)

I have a data set that contains multiple dates within each row, in the sample data the dates are published in C:E. The number of dates will vary from line to line. What I'd like to do is to create a NEW row for EACH of the dates, pasted as values, since the original rows will contain formulas. So customer 'a' would generate 3 new lines, 'b', 1 row, and 'c' 2 rows, as shown in the second image. Any help would be greatly appreciated!!


Raw Data

1657900119196.png




Desire Outcome

1657900146307.png
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hello!

How about
VBA Code:
Sub srn()
Dim rng As Range, r&, c&, rc&, cc&
Dim i&, ii&, sh As Worksheet

Set rng = Range("A1").CurrentRegion: rc = rng.Rows.Count: cc = rng.Columns.Count
Set sh = Worksheets.Add: i = 1
For r = 2 To rc
    For c = 3 To cc
        If rng.Cells(r, c) <> "" Then
            For ii = 1 To 2
                sh.Cells(i, ii) = rng.Cells(r, ii)
            Next ii

            rng.Cells(r, c).Copy
            With sh.Cells(i, 3)
                .PasteSpecial xlPasteValues
                .PasteSpecial xlPasteFormats
            End With
            i = i + 1
        End If
    Next c
Next r
End Sub
 
Upvote 0
Alternative solution with Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date1", type date}, {"Date2", type date}, {"Date3", type date}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Customer", "Value"}, "Attribute", "Value.1"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Value.1", "Date1"}})
in
    #"Renamed Columns"

Book2
ABCDEFGHI
1CustomerValueDate1Date2Date3CustomerValueDate1
2a107/1/20226/1/20226/1/2020a107/1/2022
3b5012/1/2022a106/1/2022
4c401/1/20215/1/2022a106/1/2020
5b5012/1/2022
6c401/1/2021
7c405/1/2022
Sheet1
 
Upvote 0
Solution
What version of Excel are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Hello!

How about
VBA Code:
Sub srn()
Dim rng As Range, r&, c&, rc&, cc&
Dim i&, ii&, sh As Worksheet

Set rng = Range("A1").CurrentRegion: rc = rng.Rows.Count: cc = rng.Columns.Count
Set sh = Worksheets.Add: i = 1
For r = 2 To rc
    For c = 3 To cc
        If rng.Cells(r, c) <> "" Then
            For ii = 1 To 2
                sh.Cells(i, ii) = rng.Cells(r, ii)
            Next ii

            rng.Cells(r, c).Copy
            With sh.Cells(i, 3)
                .PasteSpecial xlPasteValues
                .PasteSpecial xlPasteFormats
            End With
            i = i + 1
        End If
    Next c
Next r
End Sub
works perfectly, thanks for taking the time to put together!
 
Upvote 0
Alternative solution with Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date1", type date}, {"Date2", type date}, {"Date3", type date}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Customer", "Value"}, "Attribute", "Value.1"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Value.1", "Date1"}})
in
    #"Renamed Columns"

Book2
ABCDEFGHI
1CustomerValueDate1Date2Date3CustomerValueDate1
2a107/1/20226/1/20226/1/2020a107/1/2022
3b5012/1/2022a106/1/2022
4c401/1/20215/1/2022a106/1/2020
5b5012/1/2022
6c401/1/2021
7c405/1/2022
Sheet1
Thanks! My data has 10K+ rows, so this option was considerably quicker than VBA, didn't occur to me to use Power Query. Appreciate you giving the alternate solution!
 
Upvote 0
Thanks for updating your profile. (y)
As you have xl365 here's a formula solution
Excel Formula:
=LET(a,Sheet1!A2:B10000,b,Sheet1!C2:E10000,c,COLUMNS(b),s,SEQUENCE(c*ROWS(b),,0),ax,INDEX(a,INT(s/c)+1,{1,2}),bx,INDEX(b,INT(s/c)+1,MOD(s,c)+1),FILTER(CHOOSE({1,2,3},ax,ax,bx),bx<>""))
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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