Macro using for loop to repeat rows based on from and to date columns

haribabu

New Member
Joined
Feb 5, 2014
Messages
7
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello Excel users,
i am trying to create a macro where i can repeat rows based on from and to date columns.
So is there any way using for loop which can get the below desired output in csv by using macro?



INPUT from excel sheet
[TABLE="width: 500"]
<tbody>[TR]
[TD]From[/TD]
[TD]To[/TD]
[TD]Product[/TD]
[TD]Growth[/TD]
[/TR]
[TR]
[TD]19-09-2018[/TD]
[TD]25-09-2018[/TD]
[TD]A[/TD]
[TD]2%[/TD]
[/TR]
[TR]
[TD]23-09-2018[/TD]
[TD]27-09-2018[/TD]
[TD]B[/TD]
[TD]3%[/TD]
[/TR]
</tbody>[/TABLE]

Expected Output in CSV
[TABLE="width: 500"]
<tbody>[TR]
[TD]Date[/TD]
[TD]Product[/TD]
[TD]Growth[/TD]
[/TR]
[TR]
[TD]19-09-2018[/TD]
[TD]A[/TD]
[TD]2%[/TD]
[/TR]
[TR]
[TD]20-09-2018[/TD]
[TD]A[/TD]
[TD]2%[/TD]
[/TR]
[TR]
[TD]21-09-2018[/TD]
[TD]A[/TD]
[TD]2%[/TD]
[/TR]
[TR]
[TD]22-09-2018[/TD]
[TD]A[/TD]
[TD]2%[/TD]
[/TR]
[TR]
[TD]23-09-2018[/TD]
[TD]A[/TD]
[TD]2%[/TD]
[/TR]
[TR]
[TD]24-09-2018[/TD]
[TD]A[/TD]
[TD]2%[/TD]
[/TR]
[TR]
[TD]25-09-2018[/TD]
[TD]A[/TD]
[TD]2%[/TD]
[/TR]
[TR]
[TD]23-09-2018[/TD]
[TD]B[/TD]
[TD]3%[/TD]
[/TR]
[TR]
[TD]24-09-2018[/TD]
[TD]B[/TD]
[TD]3%[/TD]
[/TR]
[TR]
[TD]25-09-2018[/TD]
[TD]B[/TD]
[TD]3%[/TD]
[/TR]
[TR]
[TD]26-09-2018[/TD]
[TD]B[/TD]
[TD]3%[/TD]
[/TR]
[TR]
[TD]27-09-2018[/TD]
[TD]B[/TD]
[TD]3%[/TD]
[/TR]
</tbody>[/TABLE]


Thanks a lot
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try this macro:
Code:
Public Sub Create_CSV_File()

    Dim csvFile As Variant
    Dim csvData As String
    Dim lastRow As Long, r As Long
    Dim d As Date
    
    csvFile = Application.GetSaveAsFilename(InitialFileName:=ThisWorkbook.Path, FileFilter:="CSV file (*.csv), *.csv", Title:="CSV output file")
 
    If csvFile <> False Then
        With ActiveSheet
            lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
            csvData = "Date,Product,Growth" & vbCrLf
            For r = 2 To lastRow
                For d = .Cells(r, "A").Value To .Cells(r, "B").Value
                    csvData = csvData & Format(d, "dd-mm-yyyy") & "," & .Cells(r, "C").Value & "," & .Cells(r, "D").Text & vbCrLf
                Next
            Next
        End With
        Open csvFile For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
        Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , csvData
        Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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