LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Search specific column for a variable date

Rick,

Thank you so much for taking the time and breaking down how the formula
works. I have now added it to my script and I am going to go back and modify
previous sections of my code with your suggestions. :-D

- Jason

"Rick Rothstein" wrote:

CheckForDate = Date - 2 * (1 - (Weekday(Date, vbMonday) < 3))

** This is my understanding:
B/c my case statements subtract a minimum of 2 days (from whatever day it
happens to be) the first part makes sense. I get a little lost when the
Weekday(Date, vbMonday) < 3) is brought in. From what I can gather it
resets
the 'Weekday' start to Monday (which now makes the default value of
vbsunday
= 7 vs 1. )
Now b/c of that the two days where I subtract 4 instead of 2 fall inside
the
new defined range of weekday(date, vbMonday) 2 & 1 (Tuesday & Monday
respectfully) the formula would breakdown today as:

CheckForDate = Date - 2 * (1 - (Weekday(Date, vbMonday) < 3))
CheckForDate = Date (11/4/08) - 2 * (1 - (Weekday(Date, vbMonday) < 3))
CheckForDate = 11/2/08 * (1 - (Weekday(Date, vbMonday) < 3))
CheckForDate = 11/2/08 * (1 - 2)
CheckForDate = 10/31/08 *** [I think]


Your post has made my day... it is always my hope when I post some code that
the OP will actually try to decipher why it works as opposed to simply
copying it blindly.

It might be easier to understand how the code line works by expanding
expression (that is, multiply the 2 across the parentheses). If we do that,
then this...

CheckForDate = Date - 2 * (1 - (Weekday(Date, vbMonday) < 3))

becomes this...

CheckForDate = Date - 2 + 2 * (Weekday(Date, vbMonday) < 3))

The Date - 2 part you understand. As for the rest, let me first remind you
that logical expressions in VB evaluate to -1 when True. So, when the
weekday (with the week starting on Monday) evaluates to 1 or 2 (which as you
figured out occurs on Monday and Tuesday), the -1 that the logical
expression returns is multiplied by 2 and then added to the part you already
understand (remember, you are adding an negative value, so in reality it is
equivalent to a subtraction). The net result is that 2 days are subtracted
on all day except Monday and Tuesday when an additional 2 days are
subtracted as well.

--
Rick (MVP - Excel)


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find & Replace - Limit search to a specific column falena23 Excel Worksheet Functions 3 July 28th 08 03:46 PM
Search Specific Column for a matched entry Carlee Excel Programming 3 June 10th 07 05:23 PM
search column for specific cell using vba dave91 Excel Programming 1 July 30th 05 05:59 PM
search a column for a specific piece of data using vba dave91 Excel Programming 1 July 23rd 05 05:56 PM
search column for a specific piece of data using vba dave91 Excel Programming 2 July 23rd 05 02:24 PM


All times are GMT +1. The time now is 09:49 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"