Dynamically adding rows to a spreadsheet based on number of month difference between start date and end date

pmvidwans

New Member
Joined
Feb 24, 2014
Messages
3
Hello,

I am working on a spreadsheet which has column A Start Date, column B End Date and column C Cost/Value. I need to distribute the cost across the number of months evently.

Example"

Column A | Column B | Column C
1-Jan-2014 31-Dec-2014 5000
2-Feb-2014 19-Feb-2014 2000
3-Mar-2014 1-June-2014 7000

Expected Result

Column A | Column B | Column C
1 Jan 2014 31-Dec-2014 =5000/12
1 Jan 2014 31-Dec-2014 =5000/12
1 Jan 2014 31-Dec-2014 =5000/12
1 Jan 2014 31-Dec-2014 =5000/12
1 Jan 2014 31-Dec-2014 =5000/12
1 Jan 2014 31-Dec-2014 =5000/12
1 Jan 2014 31-Dec-2014 =5000/12
1 Jan 2014 31-Dec-2014 =5000/12
1 Jan 2014 31-Dec-2014 =5000/12
1 Jan 2014 31-Dec-2014 =5000/12
1 Jan 2014 31-Dec-2014 =5000/12
1 Jan 2014 31-Dec-2014 =5000/12
2-Feb-2014 19-Feb-2014 =2000/1
3-Mar-2014 1-June-2014 =7000/4
3-Mar-2014 1-June-2014 =7000/4
3-Mar-2014 1-June-2014 =7000/4


The division is based on the number of month difference between the start date and end date.

Please suggest.

Thanks in Advance...
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
maybe this?

Code:
Sub pmvidwans()

Set ws = ActiveSheet
Sheets.Add.Name = "Test"

For i = 1 To ws.Range("A" & Rows.Count).End(xlUp).Row

m1 = Format(ws.Cells(i, 1).Value, "mm")
m2 = Format(ws.Cells(i, 2).Value, "mm")
diff = (m2 - m1) + 1

For j = 1 To diff
 k = k + 1
  ActiveSheet.Cells(k, 1).Value = ws.Cells(i, 1).Value
  ActiveSheet.Cells(k, 2).Value = ws.Cells(i, 2).Value
  ActiveSheet.Cells(k, 3).Value = ws.Cells(i, 3).Value / diff
Next j
Next i
End Sub
 
Upvote 0
Hello HH,

Thanks for the response.. but it seems to be looping in itself in the J loop.

Also it is not adding additional rows below the active row based on difference.

Again for more clarity the expected output is as specified below

Start DateEnd DateTotalMonth
1-Jan-1431-Dec-14416.67January
1-Jan-1431-Dec-14416.67February
1-Jan-1431-Dec-14416.67March
1-Jan-1431-Dec-14416.67April
1-Jan-1431-Dec-14416.67May
1-Jan-1431-Dec-14416.67June
1-Jan-1431-Dec-14416.67July
1-Jan-1431-Dec-14416.67August
1-Jan-1431-Dec-14416.67September
1-Jan-1431-Dec-14416.67October
1-Jan-1431-Dec-14416.67November
1-Jan-1431-Dec-14416.67December
2-Feb-1419-Feb-142000February
3-Mar-141-Jun-141750March
3-Mar-141-Jun-141750April
3-Mar-141-Jun-141750May
3-Mar-141-Jun-141750June

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>

Hope this information helps...

Thanks in advance..
 
Upvote 0
this is exactly what I got as the result when using my code

1. the new worksheet "Test has been created?
2. has been data written to the new workbook
3. the active sheet in the beginning is the sheet with your data on it?
 
Upvote 0
My Apologies... the code is working perfectly fine.

In addition to that could you suggest how to get the exact month value in the last column ?

I tried adding this statement towards the end (within the j loop) but no help...

ActiveSheet.Cells(k, 4).Value = Format(j, "mmmm")

Please suggest..

Thanks
 
Upvote 0
Code:
Sub pmvidwans()

Set ws = ActiveSheet
Sheets.Add.Name = "Test"

For i = 1 To ws.Range("A" & Rows.Count).End(xlUp).Row

m1 = Format(ws.Cells(i, 1).Value, "mm")
m2 = Format(ws.Cells(i, 2).Value, "mm")
diff = (m2 - m1) + 1
n = m1
For j = 1 To diff
 k = k + 1
  ActiveSheet.Cells(k, 1).Value = ws.Cells(i, 1).Value
  ActiveSheet.Cells(k, 2).Value = ws.Cells(i, 2).Value
  ActiveSheet.Cells(k, 3).Value = ws.Cells(i, 3).Value / diff
  ActiveSheet.Cells(k, 4).Value = MonthName(n)
 n = n + 1
Next j
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,397
Members
448,957
Latest member
Hat4Life

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