Copy Months Between Two Dates on Other Worksheet in VBA

shellp

Board Regular
Joined
Jul 7, 2010
Messages
194
Office Version
  1. 2010
  2. 2007
Platform
  1. Windows
The code below works to get the actual dates of the start and end date as well as all dates in between. I have formulae in columns B, C and D thus the filldown portion of the code.

What I would like is to copy the MONTHS between two periods instead of dates. The start and end dates would be in the format of Apr-2017, May-2017 etc. So if I chose Apr-2017 as start period and Sep-2017 as end period I would want to see, starting in A2, Apr-2017, May-2017, Jun-2017, Jul-2017, Aug-2017 and Sep-2017. All assistance greatly appreciated.

Code:
Sub CopyDates()
  
    Dim OutRng As Range
    Dim StartValue As Variant
    Dim EndValue As Variant
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim lastrow As Long
        
   Set ws1 = Sheets("Scorecard")
   Set ws2 = Sheets("Data_1")
        
    StartValue = ws1.Range("K1").Value
    EndValue = ws1.Range("K2").Value
    
    ws2.Range("A3:B500").Clear
    Set OutRng = ws2.Range("A2")
    
     If EndValue - StartValue <= 0 Then
        Exit Sub
        End If
        ColIndex = 0
        For i = StartValue To EndValue
            OutRng.Offset(ColIndex, 0) = i
            ColIndex = ColIndex + 1
        Next
        lastrow = ws2.Range("A" & Rows.Count).End(xlUp).Row
        ws2.Range("B2:B" & lastrow).FillDown
    End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Maybe...

Code:
Sub CopyDates()
    Dim OutRng As Range
    Dim StartValue As Variant
    Dim EndValue As Variant
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim lastrow As Long
         
    Set ws1 = Sheets("Scorecard")
    Set ws2 = Sheets("Data_1")
         
    StartValue = ws1.Range("K1").Value
    EndValue = ws1.Range("K2").Value
     
    ws2.Range("A3:B500").Clear
    Set OutRng = ws2.Range("A2")
     
    If EndValue - StartValue <= 0 Then Exit Sub
     
    Dim i As Long, NumMonths As Long
    
    NumMonths = DateDiff("m", StartValue, EndValue)
    
    For i = 0 To NumMonths
         OutRng.Offset(i) = DateAdd("m", i, StartValue)
    Next
    OutRng.Resize(NumMonths + 1).NumberFormat = "mmm - yyyy"
    lastrow = ws2.Range("A" & Rows.Count).End(xlUp).Row
    ws2.Range("B2:B" & lastrow).FillDown
End Sub

Hope this helps

M.
 
Upvote 0
Thanks Marcelo! It works except in column b the title from B1 copies down, not the formula in B2 so not sure why that is. Also, what if I wanted to make the cells of K1 and K2 drop down lists instead of the user typing in dates? I ask because I did that and now the above doesn't work so I assume I am referencing it wrong. Thanks.
 
Upvote 0
It worked for me (months in Portuguese: abr = apr; mai = may, etc)

Say you have:

Scorecard
K
1
abr-2017
2
set-2017

<tbody>
</tbody>


Data_1
A
B
1
Header1
Header2​
2

<tbody>
</tbody>

Formula in B2
=IF(A2="","",EOMONTH(A2,0))

Macro
Code:
Sub CopyDates()
    Dim OutRng As Range
    Dim StartValue As Variant
    Dim EndValue As Variant
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim lastrow As Long
         
    Set ws1 = Sheets("Scorecard")
    Set ws2 = Sheets("Data_1")
         
    StartValue = ws1.Range("K1").Value
    EndValue = ws1.Range("K2").Value
     
    ws2.Range("A3:B500").Clear
    Set OutRng = ws2.Range("A2")
     
    If EndValue - StartValue <= 0 Then Exit Sub
     
    Dim i As Long, NumMonths As Long
    
    NumMonths = DateDiff("m", StartValue, EndValue)
    
    For i = 0 To NumMonths
         OutRng.Offset(i) = DateAdd("m", i, StartValue)
    Next
    OutRng.Resize(NumMonths + 1).NumberFormat = "mmm - yyyy"
    lastrow = ws2.Range("A" & Rows.Count).End(xlUp).Row
    With ws2.Range("B2:B" & lastrow)
        .FillDown
        .NumberFormat = "dd/mm/yyyy"
    End With
End Sub


Data_1 (after the code is run)

A
B
1
Header1​
Header2​
2
abr - 2017​
30/04/2017​
3
mai - 2017​
31/05/2017​
4
jun - 2017​
30/06/2017​
5
jul - 2017​
31/07/2017​
6
ago - 2017​
31/08/2017​
7
set - 2017​
30/09/2017​
8

<tbody>
</tbody>


M.
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,446
Members
449,083
Latest member
Ava19

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