I would like to get the week day number from a date.
E.g. : if it is a Monday returns 1, if it is a Tuesday it returns 2, ... if it is a Sunday it returns 7.
I was wondering if there is a direct function to obtain this, for instance:
DayNumber = Format(Date, <format unknown to me>)
The closest I could get is to use the function:
DayNumber = Format(Date, "dddd")
then loop through a predefined list of possible names, but I still wonder if there is a simpler way?
A bit of googling would have found the WEEKDAY function
weekday(date,vbMonday)
Related
I need help with a formula that will add [Extension], a choice field of 30, 45, or 60, to [Created], and return a date [Extension Weekday].
BUT, that future date must be a weekday.
Example: [Created] + [Extension] = [Extension Weekday], but if the result is a Saturday or Sunday, calculate it as the next Monday.
Thanks!
You can use following formula:
=A2+B2+((WEEKDAY(A2+B2,2)=6)*2)+((WEEKDAY(A2+B2,2)=7)*1)
First weekday = 1 = Monday, last = 7 = Sunday. Change WEEKDAY function second parameter if needed.
You might use following functions:
TEXT() : =TEXT(20/02/2020;"dddd") shows the name of that day.
IF(condition;result)
OR(condition1;condition2)
So you'll need something like:
=IF(OR(TEXT(<date>;"dddd")="Saturday";TEXT(<date>;"dddd")="Sunday");"Monday";TEXT(<date>;"dddd"))
The formatting, day naming, ... might be language dependent, so I'll advise you to do some experimenting, but this gives you a start.
I have a small code that gets the Day Name of the Dates listed in Excel. But the problem is, it's not getting the right Day Name(e.g. Tuesday).
For Example:
sDayName = Format(Day(11/1/2016), "dddd")
Then the output produces an incorrect Day Name which is:
sDayName = "Sunday"
when it's supposed to be "Tuesday".
Thanks for the help guys.
For your specific request, assuming your date string is formatted as your regional setings:
sDayName = Format("11/01/2016", "dddd")
If you want the weekday for today:
sDayName = Format(Date, "dddd")
If you want the weekday for any date in any regional settings
sDayName = Format(DateSerial(2016, 11, 4), "dddd")
'change 2016, 11, 4 for your year, month, day values
:)
WEEKDAY Function can be used on this topic and in Vba Codes . For example:
Label2.Caption = WeekdayName(Weekday(TextBox2, 0), False, 0)
I used this function in when I created userform about date entry to active cell.
i use the answer kadrleyn wrote.
But you have to pay attension that the string returned from
WeekdayName(Weekday(TextBox2, 0), False, 0)
is not trimmed so if you want to use it in if statement you have to use trim function.
ps. i could not add comment to his/her answer because of my reputation.
What excel code will return the 14th day of the month if it is a workday and then if it is not a workday, then return the next day that is a workday.
I can't get the workday function to work correctly, because whenever I use it, it doesn't seem to know how to handle that.
For Example: 10/14/2019 is a holiday
=Workday (10/14/2019,0,hdays) = 10/14/2019
=Workday (10/14/2019,1,hdays) = 10/15/2019 = desired solution since 10/14 is a holiday
however, if I have a date that is not a weekend or holiday, that middle variable of "1"
will add a day.
=Workday(11/14/2019,0,hdays) = 11/14/2019 = works here but not in example above.
=Workday(11/14/2019,1,hdays) = 11/15/2019 = wrong....I want 11/14
So I need the 14th if it is a workday, and if not, then I need the next workday.
Start with the day before and add 1:
=Workday("10/14/2019"-1,1,hdays)
I'm having trouble writing code in VBA that would allow me to input any given date then have an output of the current work week. I need a restriction of if the date is Sunday through Tuesday, it will keep that current work week and year but if the date is Wednesday through Saturday, then the next work week and year will show. For example, I'm looking to input (5/28/19) and have an output of 201922 or an input of (5/29/19) with an output of 201923 even though its technically the same work week.
Before getting too in depth, I do have a working function that provides the year and work week, but I'm trying to adapt the function or add a separate function that will change in to the next work week according to the given date.
I'm new to VBA but have tried to do a little research over the last few days. I was thinking that I could somehow have one input of the date then have two outputs where one would be the year and work week then the other would be the number associated with that date (1 for Sunday, 2 for Monday, and so on). I tried to create an if then statement that says if the number associated with that date is 1, 2, or 3 then the workweek would stay the same. If it was any other number then 1 would be added to that work week so it would move to the next one. I'm having trouble with trying to make two outputs and have them connected, if that makes any sense.
This is the code that I tried to create, but continuously failed at making. The function that gives the correct work week (without the adaptation of the work week based on the date weekday) is WWV1
Function WWV2(WeekdayName As Integer)
Dim WWV1 As Integer
If WeekdayName(Date) = 1 Or 2 Or 3 Then WWV1 = WWV1
Else: WWV1 = WWV1 + 1
End Function
This provides the cell with #NUM! when I use the function in that cell, which I assume is because I need to somehow connect the two functions.
how about this:
=YEAR(A1) & TEXT(WEEKNUM(A1,13),"00")
WeekNum returns the weeknumber with 13 saying it starts on Wednesday:
VBA
wkcd = Year(Range("A1")) & Format(Application.WeekNum(Range("A1")),"00")
With the limitations in the Weeknum standard function in Power Query, does anyone have any M-Code with which a function can be invoked to give different Weeknum return types (specifically, in Excel it's be Weeknum (date,14)).
I found this to give me ISO weeknums:
let
Thursday = Date.AddDays(DateParameter,3-Date.DayOfWeek(DateParameter,Day.Monday)),
Jan1 = #date(Date.Year(Thursday),1,1),
Days = Number.From(Thursday - Jan1),
Result = Number.RoundDown(Days/7)+1
in
Result
But I cannot get my around how to modify this to make the weeks start on a Thursday (I'm sure it'll be simple!)
Thanks in advance for your help - much appreciated.
Function Date.WeekOfYear has an undocumented second argument in which you can provide the first day of the week, i.c. Date.WeekOfYear(date,Day.Thursday).
Note: if January 1 is a Wednesday, then January 2 is aleady week 2 (so not like ISO).