Just starting to write VBA here, raw beginner.
I'm looking for help in fine-tuning my displayed date. With the help of examples I found online, I have a module to put the name of the last month, and year, into a cell in my Totals worksheet, and it does what I need to get by.
To make it perfect, though, I'd like to change what date info is displayed.
This is Excel in Office 365. I've searched for examples online and read tutorials about using DateAdd but can't find this anywhere. (Part of the problem is I'm so new, I don't know how to phrase things in vba-speak to find good answers!)
What I use now:
Sub PreviousMonthBasedOnCurrentMonth()
Dim ws As Worksheet
Set ws = Worksheets("Totals")
ws.Range("A1") = Format(DateAdd("m", -1, Date), "mmmm yyyy")
End Sub
This now inserts "February 1, 2019" into the cell - which is enough data for me to confirm which month I'm looking at. But ideally I'd like "February 1-28, 2019" (Or, say, "October 1-31, 2019" if I'm running this while in Nov).
How do I get the 'first day of month - last day of month' portion to display? Is that part of DateAdd, or more coding from scratch?
Not sure how to write something like that myself. Pointers to where to start would be great.
Try something like this:
Dim dt, dt2
dt = DateAdd("m", -1, Date) 'this time last month
dt2 = DateSerial(Year(Date), Month(Date), 1) - 1 'last day of previous month
Debug.Print Format(dt2, "mmmm") & " 1-" & Day(dt2) & " " & Format(dt2, "yyyy")
Note Excel will not see this as a date though...