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
FromToProductGrowth
19-09-201825-09-2018A2%
23-09-201827-09-2018B3%

<tbody>
</tbody>

Expected Output in CSV
DateProductGrowth
19-09-2018A2%
20-09-2018A2%
21-09-2018A2%
22-09-2018A2%
23-09-2018A2%
24-09-2018A2%
25-09-2018A2%
23-09-2018B3%
24-09-2018B3%
25-09-2018B3%
26-09-2018B3%
27-09-2018B3%

<tbody>
</tbody>


Thanks a lot
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

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