Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-21-2024, 06:20 PM
Karen615 Karen615 is offline Date Formula Windows 11 Date Formula Office 2021
Competent Performer
Date Formula
 
Join Date: Jun 2011
Location: Chicago
Posts: 145
Karen615 is on a distinguished road
Default Date Formula

Hello,



In my spreadsheet (in cell A4), I have the formula =TODAY(). In cell A3, the formula is: =A4-1. The formula in cell A3 works well Tue-Fri, but when I come into work on Monday, I need cell A3 to display a Friday date and not a Sunday date. Is there any way to modify this formula so it skips over weekends?

Your help is greatly appreciated,
Karen
Reply With Quote
  #2  
Old 11-21-2024, 07:53 PM
macropod's Avatar
macropod macropod is online now Date Formula Windows 10 Date Formula Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,382
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

For a dynamic formula that updates daily, you could use:
Quote:
=TODAY()-1-(MOD(TODAY(),7)=2)*2
For a formula that references the date in another cell, you could use:
Quote:
=A4-1-(MOD(A4,7)=2)*2
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-22-2024, 02:24 AM
ArviLaanemets ArviLaanemets is offline Date Formula Windows 8 Date Formula Office 2016
Expert
 
Join Date: May 2017
Posts: 932
ArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant future
Default

Or in A3 use (assumed the worksheet is never used at weekends, and you have to cope with state holidays otherwise)
Code:
=TODAY() - IIF(WEEKDAY(TODAY(),2)=1,3,1)
In case you need to take holidays also into account, you need a hidden calendar table with state holidays marked, and with a calculated column which returns the number of days to previous workday in case of holiday or weekend (p.e. for my country, this may be up to 5 days if I remember correctly), and 0 in case of workday. The formula in A3 will calculate the previous workday based on this hidden table.
Reply With Quote
  #4  
Old 11-22-2024, 06:35 AM
Karen615 Karen615 is offline Date Formula Windows 11 Date Formula Office 2021
Competent Performer
Date Formula
 
Join Date: Jun 2011
Location: Chicago
Posts: 145
Karen615 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
You could use:
For a dynamic formula that updates daily, you could use:
Quote:
=TODAY()-1-(MOD(TODAY(),7)=2)*2
For a formula that references the date in another cell, you could use:
Quote:
=A4-1-(MOD(A4,7)=2)*2
=TODAY()-1-(MOD(TODAY(),7)=2)*2

Thank you so much for your help. If you have a chance, would you please explain this formula in layman's terms?

Your help is greatly appreciated.
Thank you,
Karen
Reply With Quote
  #5  
Old 11-22-2024, 06:42 AM
Karen615 Karen615 is offline Date Formula Windows 11 Date Formula Office 2021
Competent Performer
Date Formula
 
Join Date: Jun 2011
Location: Chicago
Posts: 145
Karen615 is on a distinguished road
Default

Quote:
Originally Posted by ArviLaanemets View Post
Or in A3 use (assumed the worksheet is never used at weekends, and you have to cope with state holidays otherwise)
Code:
=TODAY() - IIF(WEEKDAY(TODAY(),2)=1,3,1)
In case you need to take holidays also into account, you need a hidden calendar table with state holidays marked, and with a calculated column which returns the number of days to previous workday in case of holiday or weekend (p.e. for my country, this may be up to 5 days if I remember correctly), and 0 in case of workday. The formula in A3 will calculate the previous workday based on this hidden table.
Thank you so much for your help. If you have a chance, would you please explain this formula in layman's terms?

Also, I do need to deal with holidays. I'm not sure how to set up that hidden calendar.

Your help is greatly appreciated.
Thank you,
Karen
Reply With Quote
  #6  
Old 11-22-2024, 08:29 AM
ArviLaanemets ArviLaanemets is offline Date Formula Windows 8 Date Formula Office 2016
Expert
 
Join Date: May 2017
Posts: 932
ArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant future
Default

Quote:
Originally Posted by Karen615 View Post
Thank you so much for your help. If you have a chance, would you please explain this formula in layman's terms?
The formula returns the date of previous Friday (TODAY-3), when the current date is Monday, i.e. the weekday number of current date is 1 (I used WEEKDAY() with 2nd argument 2 to get weekdays numbered from 1 = Monday to 7 = Sunday).
For other workdays (from Tuesday to Friday, with weekday numbers from 2 to 5), the formula returns the date of previous workday (TODAY -1).
As I declared in my precvious post, the formula in not meant to use at weekends.

In case you want this formula to work when the workbook is opened at weekends (but ignoring holidays) too, you have to use instead
Code:
=TODAY() - CHOOSE(WEEKDAY(TODAY(),2),3,1,1,1,1,1,2)
The CHOOSE() returns from values at 2nd to last parameters, the one with order number equal to value of 1st parameter

Quote:
Also, I do need to deal with holidays. I'm not sure how to set up that hidden calendar.
On separate sheet, create a table with header for column A (e.g. in cell A1) something like "Date". Into cell A2 enter the start date (e.g. January 1st of current year), into cell A2, enter the next date (e.g. January 2nd of current year), select both dates, and drag down (from right lower edge) to fill the column with as much dates you think you will need;
Add another column (e.g. column B with header "Weekday"), and fill it with weekday numbers using WEEKDAY() formula;
Add another column (e.g. column C with header "Holiday"), and fill it with number 1 for every holiday in daterange of your table. When the date isn't a holiday, leave it empty, or enter 0;
Add another column (e.g. column D with header "PreviousWD"). Based on info in columns B:C, fill this column with 0 when the date is workday and it is not a holiday. Otherwise enter, how many days back was last workday (1, or 2, or ... days). It is possible to create a formula for this column, but I'm afraid this will be too complex for you, and I don't have access to Excel currently, and even having it, the creating such formula will take hours for me too.

Now either define this calendar table as Table with name like tCalendar, or select the whole datarange of created table, and define it as Name like nCalendar (I'll continue with defined Name below).

On your original sheet, into cell A3 enter the formula like
Code:
=(TODAY()-1) - VLOOKUP((TODAY()-1),nCalendar,4,0)
The 3rd parameter of VLOOKUP() in formula indicates the 4th column of range nCalendar - i.e. the VLOOKUP() returns the matching number in column PreviousWD.

After checking the formula for previous workday working, you can hide the sheet with calendar table you created.

Last edited by ArviLaanemets; 11-22-2024 at 11:56 AM.
Reply With Quote
  #7  
Old 11-22-2024, 09:33 AM
p45cal's Avatar
p45cal p45cal is offline Date Formula Windows 10 Date Formula Office 2021
Expert
 
Join Date: Apr 2014
Posts: 948
p45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond repute
Default

In the attached, formula in cell B4:
=WORKDAY(A4,-1,Holidays)
You could have more control with
=WORKDAY.INTL(A4,-1,1,Holidays)
in cell B4 if WORKDAY.INTL is available to you in your version of Excel.
Holidays is the name of a table containing a list of holiday dates.

For demonstration purposes, in the attached:
The date in cell A4 is not =TODAY() but fixed to today's date (22/Nov/2024) but this will still work if you do use =TODAY()

Also, I've added a few more dates below cell A4 and used the same formulae in columns B & C. I've added conditional formatting to column A to highlight holiday dates and weekends so that you can confirm the formulae give the correct results.

2024-11-22_162713.jpg
Attached Files
File Type: xlsx msofficeforums53020.xlsx (11.3 KB, 3 views)
Reply With Quote
  #8  
Old 11-22-2024, 01:23 PM
macropod's Avatar
macropod macropod is online now Date Formula Windows 10 Date Formula Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,382
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Karen615 View Post
=TODAY()-1-(MOD(TODAY(),7)=2)*2

Thank you so much for your help. If you have a chance, would you please explain this formula in layman's terms?
Dates in Excel are stored as numbers, going back to 1 Jan 1901, and incrementing by 1 for each day.

When we divide a date by 7, the remainder tells us what day of the week it is. If the remainder is:
0 - the day is a Saturday
1 - the day is a Sunday
2 - the day is a Monday
3 - the day is a Tuesday
4 - the day is a Wednesday
5 - the day is a Thursday
6 - the day is a Friday

The MOD function returns the remainder of a number and its divisor. Hence, if the remainder returned by MOD function of a date divided by 7 is 2, the date is a Monday.

So:
TODAY()-1
returns yesterday
MOD(TODAY(),7)=2
is a True/False test for Monday and, if TRUE
-(MOD(TODAY(),7)=2)*2
deduct another 2 days from today.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 11-30-2024, 08:31 AM
Karen615 Karen615 is offline Date Formula Windows 11 Date Formula Office 2021
Competent Performer
Date Formula
 
Join Date: Jun 2011
Location: Chicago
Posts: 145
Karen615 is on a distinguished road
Default

Thank you so much for your detailed reply! Your explanation is incredibly helpful, and I truly appreciate the time and effort you put into this. People on this site are amazing!
Happy holidays!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Date Formula Issue matteu1 Mail Merge 1 07-23-2018 04:53 AM
Formula for looking at 1 cell with a date and then comparing it to four date ranges LearningMom Excel 3 10-27-2017 12:44 PM
Date Formula Formula to return a date caz46 Excel 3 04-15-2015 08:59 AM
Date Formula Need a date formula MPAVLAS Excel 3 08-12-2010 10:04 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:26 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft