I have added text in to my email that goes like:
"please provide numbers for MMMM month end" - where MMMM is the name of the previous month.
So it it's April today, MMMM will show March.
I have the following code:
Dim newDate: newDate = DateAdd("M", -1, Now)
But the result comes out to be 27/03/2017 16:37:58
I want it to show March.
Any suggestions?
Format the return as "MMMM":
Dim newDate: newDate = Format(DateAdd("M", -1, Now), "MMMM")
If set to True, the month name is abbreviated e.g. Apr
newDate = MonthName(Month(DateAdd("m", -1, Date)), False)
This will ensure the month is in English irrespective of the regional settings on the computer. This will be helpful for those who are releasing an Excel VBA tool worldwide.
previousmonth = WorksheetFunction.Text(DateAdd("m", -1, Date), "[$-409]mmmm")
For abbreviated months eg: Apr use
previousmonth = WorksheetFunction.Text(DateAdd("m", -1, Date), "[$-409]mmm")
Related
01/02/2018 12:25:00 PM
I have the above date which is in an Excel spreadsheet's B1 cell. The date is 01 February 2018 but the VBA code below
thedate = CDate(Application.Cells(linecount, 2))
converts this to 02 January 2018
What VBA code do I use to have this remain 01 February 2018?
If you want to use VBA to return a Date data type value of string 01/02/2018 12:25:00 PM representing 01 February 2018 in your local regional date settings (in a message box), you could use:
Dim dt as String
dt = "01/02/2018 12:25:00 PM"
MsgBox DateSerial(Mid(dt, 7, 4), Mid(dt, 4, 2), Left(dt, 2))
† This is how it displays for me. See note below.
If you actually want a string to be returned (in a messages box) in the specific format you mentioned, "dd MMMM yyyy", you could use:
Dim dt as String
dt = "01/02/2018 12:25:00 PM"
MsgBox Format(DateSerial(Mid(dt, 7, 4), Mid(dt, 4, 2), Left(dt, 2)), "dd MMMM yyyy")
If you wanted to store this data (including the time), or use it for calculations, then the proper way to do it would be more like:
Dim strMyDate as String, dtMyDate as Date
strMyDate = "01/02/2018 12:25:00 PM"
dtMyDate = DateSerial(Mid(strMyDate, 7, 4), Mid(strMyDate, 4, 2), _
Left(strMyDate, 2)) + TimeValue(Mid(strMyDate, 11))
MsgBox dtMyDate
† Note that these MsgBox's are displaying date/time's based on my system's Windows settings (shown below), shown here, where they could be adjusted if required. (Hence, the results above will appear differently for you.)
Open these settings by hitting type region, and hit ᴇɴᴛᴇʀ.
(Click to enlarge)
The DateTime is actually stored in Excel as a number where 0 = December 30, 1899 and +1 = +1 day, so your example date of 01 February 2018 12:25:00 PM is actually only a formatted representation of the DateTime serial number, in this case, 43132.5173611.
More Information:
MSDN : DateSerial function (VBA)
MSDN : Format function (VBA)
MSDN : TimeValue function (VBA)
Microsoft.com : How to use dates and times in Excel
Looking to change this Microsoft Word VBA Code so that the expiration date is always every Monday of the week, not a specific date:
Sub MyMacro()
ExpirationDate = #6/1/2013#
If Now() < ExpirationDate Then
'Rest of macro goes here
End if
End Sub
Any thoughts on how to do this would be great :)
If Weekday(Date) = 2 Then ... 'Monday
Public Function FindMonday(dt As Date) As Date
Do Until WeekdayName(Weekday(dt)) = "Monday"
dt = DateAdd("d", 1, dt)
Loop
FindMonday = dt
End Function
ExpirationDate = (Date + 7) - (Weekday(Date) - Weekday(vbMonday))
This formula will always return the date of next Monday, as specified by "+7"
You don't need to declare the variable Expirationdate.
If Date < (Date + 7) - (Weekday(Date) - Weekday(vbMonday)) Then
will do the job.
Note that Now returns a date/time value whereas Date returns a date integer. If time is of the essence you would have to add it in. Add 0.5 to the above formula to fix the expiration time on next Monday, 12 noon.
I'd usually use this function:
Public Function PreviousMonday(CurrentDate As Date) As Date
PreviousMonday = CurrentDate - Weekday(CurrentDate - 2)
End Function
You can then call it as:
PreviousMonday(Date()) - would return 24/07/2017 if entered today (28th).
PreviousMonday(CDATE("1 July 2017")) - would return 26/06/2017
PreviousMonday(42430) would return 29/02/2016 (42430 = 1st March 2016).
report out of account system comes out with month like "FEB 2017". I need to convert that string to a date that is the end of the month like 02/28/2017. Any ideas?
SELECT LastDayInMonth(DateValue(Mid("Feb 2017", 1, 3) & " 1, " &
Mid("Feb 2017", 5, 4))) AS LastDayInMonth
FROM yourTable
Explanation:
The concatenated term inside the call to DateValue() will be Feb 1, 2017, and will evaluate to the same date, at least for the sample data I used. In general, it will be the first day of the month for the data you showed us. Then, we use LastDayInMonth() to shift that date to the last day of the same month.
You can also use native functions, adding one month, subtracting one day:
MonthYear = "FEB 2017"
Ultimo = DateAdd("d", -1, DateAdd("m", 1, CDate("1 " & MonthYear)))
Ultimo -> 2017-02-28
How can I get the ISO week number of some date in VBScript or VBA?
First, note that:
It is important to report the week year along with the week number, as the date's year could be different.
Several Windows components contain a bug for some years' last Monday.
In my experience the simplest, clearest and most robust way to compute this is:
Sub WeekNum(someDate, isoWeekYear, isoWeekNumber, isoWeekDay)
Dim nearestThursday
isoWeekDay = WeekDay(someDate, vbMonday)
nearestThursday = DateAdd("d", 4 - Int(isoWeekDay), someDate)
isoWeekYear = Year(nearestThursday)
isoWeekNumber = Int((nearestThursday - DateSerial(isoWeekYear, 1, 1)) / 7) + 1
End Sub
This also returns the ISO day of the week, counting from 1 for Mondays.
Enter any date into A1 cell, then run following code...
Range("A2").FormulaR1C1 = "=INT((R1C-DATE(YEAR(R1C-WEEKDAY(R1C-1)+4),1,3)+WEEKDAY(DATE(YEAR(R1C-WEEKDAY(R1C-1)+4),1,3))+5)/7)"
You can get it via DatePart() VBA function:
Sub IsoWeek()
Dim isoWeekNum As Byte
Dim myDate As Date
myDate = DateValue("01-01-2022")
isoWeekNum = DatePart("ww", myDate, vbMonday, vbFirstFourDays)
If isoWeekNum > 52 Then ' Bug check (to avoid the bug with Mondays)
If Format(myDate + 7, "ww", vbMonday, vbFirstFourDays) = 2 Then isoWeekNum = 1
End If
Debug.Print isoWeekNum
End Sub
I have a report in SSRS. In my main report I am linking to another report using a link action.
I need to pass in two variables to linked report. One for startdate and one for enddate. I need to pass in start date as first day of current month 12.00am and enddate as yesterday 1259pm
Trying to do this with an expression. Any help appreciated.
I can do this with sql as below, but need to convert this to expression used in SSRS.
Set #startdate = DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)
Set #enddate = DATEADD(ms,-3, DATEADD(day, DATEDIFF(day,0,GETDATE()),0))
try using expression
First Day of current month
=DateSerial(Year(Now()), Month(Now()), "1")
Previous Day (Yesterday)
=DateSerial(Year(Now()), Month(Now()), Day(Now())).AddDays(-1) & " 23:59:59"