# Excel/Excel Question

Question
I work in Excel 2007 and I have a problem.  I have four different dates.

The first date is a review date.

The second date is 90 days after the review date.  The second date does not fall on a weekend nor a holiday.  If this occurs, the second date formula is created to use the weekday before the weekend or holiday. =IF(ISERROR(MATCH(A2+90,\$E\$2:\$E\$40,0)),IF(WEEKDAY(A2+90)=7,A2+90-1,IF(WEEKDAY(A2+90)=1,A2+90-2,A2+90)),IF(WEEKDAY(A2+90-1)=7,A2+90-2,IF(WEEKDAY(A2+90-1)=1,A2+90-3,A2+90-1)))

The third date is 180 days after the second date.  Like the second date, it does not fall on a weekend nor a holiday.  If this occurs, the third date date formula is created to use the weekday before the weekend or holiday. =IF(ISERROR(MATCH(A2+180,\$E\$2:\$E\$40,0)),IF(WEEKDAY(A2+180)=7,A2+180-1,IF(WEEKDAY(A2+180)=1,A2+180-2,A2+180)),IF(WEEKDAY(A2+180-1)=7,A2+180-2,IF(WEEKDAY(A2+180-1)=1,A2+180-3,A2+180-1)))

My problem:  The fourth date is 180 days after the third date and sometimes the fourth date will fall on day after the third date.
=IF(ISERROR(MATCH(C2+180,\$E\$2:\$E\$40,0)),IF(WEEKDAY(C2+180)=7,C2+180-1,IF(WEEKDAY(C2+180)=1,C2+180-2,C2+180)),IF(WEEKDAY(C2+180-1)=7,C2+180-2,IF(WEEKDAY(C2+180-1)=1,C2+180-3,C2+180-1)))

I would like to have a formula for the fourth date so if the fourth date falls a day after the third date, that the fourth date should be the same as the third date. (e.g., third date = 05/03/13; fourth date = 05/04/13, but should be 05/03/13).

Thanks for the help.

I'd use a custom function like

Function daysBefore(startDate As Date, rngHolidays As Range) As Date
'return the latest date that's no later than start date,
'and not on a holiday or weekend
daysBefore = startDate
Do Until IsError(Application.Match(daysBefore, rngHolidays, 0)) And _
(Application.Weekday(daysBefore, 2) < 6)
daysBefore = daysBefore - 1
Loop
End Function

Then, assuming holiday dates are listed in A1:A2, and your looking for the date 180 days after cell C2, but not falling on a weekend or holiday, use the formula

=daysBefore(C2+180,A1:A20)
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

#### Stuart Resnick

##### Expertise

I can answer questions relating to MS Excel formulas, or to programming with vba (Visual Basic for Applications) in the Excel environment. Please follow the following guidelines: your question should focus on one specific issue you want to learn. It's beyond the scope of this free service for me to create entire projects or complex vba solutions for you from scratch. You should be able to do most of the work yourself, and come here when you need help with a specific point you're stuck on. ALWAYS include a simple, concrete example illustrating what you want to learn. Explain this example in detail in the text of your question (what data is in which cells of which sheets, etc). Be very precise about the results you want, using this sample to make the logic clear. Always keep these examples SIMPLE. Never e.g. use 18 worksheets in your example if using 2 or 3 will do. Never use ranges like AI567:BB865 if using a range like A1:B3 will do. Thanks.

##### Experience

As a consultant, I've designed Excel tools since the 90s, working for the Federal Reserve Bank, AT&T, and (currently) Gap Inc.

Education/Credentials
My only "education" comes from 2 decades of doing spreadsheet/programming work, with major SF Bay Area corporations such as AT&T, Federal Reserve Bank, and Gap Inc.