Macro to autofill month end dates between start date and end date

AndreDo

New Member
Joined
Nov 4, 2019
Messages
7
Hi Guys,

I found a macro in this forum that list all the dates between end date and start date but I can't seem to figure out how to make it only display only month end dates! Can any excel ninja here help me with this?

Sub FillDates()
Dim StartD As Date, EndD As Date

StartD = Worksheets("Sheet1").Range("B1").Value
EndD = Worksheets("Sheet1").Range("B2").Value


For Row = 1 To EndD - StartD
Cells(Row, 1) = StartD + Row - 1


Next Row

End Sub

Thank you in advance,
Dre
 
hahahaha this forum is awesome! one last question. how do i sort the results so that the newest date starts first? i tried every combo of applying the sort command but keeps getting a debug error.

For example it's currently populating like this:

9/30/2018
10/31/2018
11/30/2018

How do i sort to be like this:
11/30/2018
10/31/2018
9/30/2018

thank you in advance.

lastly, is there a good book to read up on VBA?
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Here is my code with the sort function included within it...
Code:
Sub FillDates()
  Dim Arr As Variant
  Arr = Evaluate("IF({1},EOMONTH(B1,ROW(1:" & 1 + DateDiff("m", [B1], [B2]) & ")-1))")
  With Range("A1").Resize(UBound(Arr))
    .NumberFormat = "m/d/yyyy"
    .Value = Arr
    [B][COLOR="#0000FF"].Sort .Cells(1), xlDescending[/COLOR][/B]
  End With
End Sub
 
Upvote 0
I made the adjustments to the code:

Code:
Sub FillDates()
  Dim StartD As Date, EndD As Date, i As Long, n As Long
  StartD = Worksheets("Sheet1").Range("B1").Value
  EndD = Worksheets("Sheet1").Range("B2").Value
  n = DateDiff("m", WorksheetFunction.EoMonth(StartD, -1) + 1, WorksheetFunction.EoMonth(EndD, -1) + 1)
  For i = 1 To n + 1
    Cells(i, 1) = WorksheetFunction.EoMonth(StartD, i - 1)
  Next
  Range("A1", Range("A" & Rows.Count).End(xlUp)).Sort Range("A1"), xlDescending
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,986
Members
452,541
Latest member
haasro02

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