+ Reply to Thread
Results 1 to 9 of 9

VB code for looking between two dates for certain criteria

  1. #1
    Registered User
    Join Date
    01-15-2013
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    53

    VB code for looking between two dates for certain criteria

    I have a feeling that this one is very simple, but I can't crack it! And unfortunately I can't attach an example file because it is highly confidential and would take forever to scrub it clean, so I'll do my best in describing it.

    On worksheet A I have a series of dates and a location:

    Date Location
    05/07/2014 Zoo
    09/07/2014 Farm
    12/07/2014 Cinema

    This is a series of trips that are planned to occur. In order to occur there is a criteria which is recorded seperately and is coded as '1' = criteria were broken and '2' = criteria were not broken. The criteria for each trip must be met from the date of the last trip until the day before the next trip - so using the above example, to get the farm, the criteria must be met from 05/07/2014 until 08/07/2014. In practical terms, there must be no '1' in the desingated column that records the criteria - fairly simple.

    But there is an allowance of one case of the criteria being broken, which is recorded seperately ('1' = allowance used and '2' allowance not used). This allowance resets after every potential trip (whether the trip was earned or not). This is all displayed as a 'status' on Worksheet B.

    So in terms of practical VB code I'm looking for:
    Start with row A on worksheet A - copy the date and location onto worksheet B (the next trip)
    On opening the file, take the date of the last trip (or in the case of the first trip, the designated start date)
    Look into Column J (between date of last trip and date of next trip) to see if allowance has been used (a '1')
    Look into column K (between the two dates) to see if the criteria has been broken on more than one ocasison (two '1')
    OR if possible - compare column J and K (between the two dates) to see if a criteria was broken AFTER the allowance has been used

    Then, look to see if today's date is the same date as the next trip, if it is, move the reference down to the next rown (A3, A4, etc) on Worksheet A so that the status now reflects the next trip, and the code now looks between the two new dates.

    I hope that makes sense? I have an idea on how to do most of it, but its the comparing between two dates and the different criteria that is stmuping me.

    Thanks for any help.

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: VB code for looking between two dates for certain criteria

    Please Login or Register  to view this content.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Registered User
    Join Date
    01-15-2013
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    53

    Re: VB code for looking between two dates for certain criteria

    Thanks for the reply. So this is what I have:
    Please Login or Register  to view this content.
    This means that the status page shows the date and location of the next trip which is copied from the table listing the trip plan. If today's date is the date of the trip, then it moves date_ref by 1 (via H1) and replaces the date/location so that it is now displaying the data for the next trip.

    As far as I can tell, that all works perfectly. You would have to check the previous night if the trip is going ahead as once you hit midnight, it will begin displaying for the next trip (not the one happening that day) - but that's a procedural thing and not a problem.

    The problem I am having now is this:
    I have one column which will have a number in it, which is the number of times that the criteria for the trip have been broken (it may be broken more than once per record made). I am unsure how to construct the IF statements to look at that column which also maintaining the criteria of being between the two dates - the problem is, how do I get the IF statement to consider a vast range of cell references (as I'll want to it compare all the possible dates, which will number in the hundreds). Something like:

    If (cells A1:A3000) >= (date of last trip) AND (cells A1:A3000) < (Date of next trip) Then
    Sum of (Criteria column) between those two dates
    If (Sum of criteria column) > 1 Then '(if it totals more than 1, then the allowance was used up and then critieria broken again)
    Worksheet A (cell which says if criteria was broken more than once) = Yes '(therefore making the leave state = Not Achieved)
    Else
    Worksheet A (cell for criteria) = No '(therefore making the state = Currently Achieved)

    it's the first three lines I'm not sure what the exact syntax should be to get it to consider all the different If criteria in the right order.

    Thanks again.
    Last edited by Sinnie; 07-02-2014 at 07:53 AM.

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: VB code for looking between two dates for certain criteria

    Are you still using Excel 2003?

  5. #5
    Registered User
    Join Date
    01-15-2013
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    53

    Re: VB code for looking between two dates for certain criteria

    Yep, it's excel 2003.

  6. #6
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: VB code for looking between two dates for certain criteria

    Ok, i was going to suggest using some of the IFS extensions on the normal formula using 2007 onwards.

    You can do it using an array formula then. I've attached a workbook showing what they do.

    You can digest these a bit, and then look to adjust, but you can use them in VBA, to get the results for your needs :o)
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    01-15-2013
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    53

    Re: VB code for looking between two dates for certain criteria

    Thank you so much, that works perfectly. A nice simple solution.

    I've tested it as much as I think I can, the real bit will be when I unleash it on other people.

    Much appreciated.

  8. #8
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: VB code for looking between two dates for certain criteria

    No problems are you happy with whats going on in the formula?

  9. #9
    Registered User
    Join Date
    01-15-2013
    Location
    UK
    MS-Off Ver
    Excel 2003
    Posts
    53

    Re: VB code for looking between two dates for certain criteria

    Yes indeed. I made a couple of changes to fit it into the actual file, but otherwise it was perfect.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Return dates that match a single criteria (a countif criteria)
    By nickmax1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-07-2014, 12:43 PM
  2. [SOLVED] Sum values between two dates with certain criteria
    By dmcarthur212 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 02-21-2013, 02:02 PM
  3. 3 Criteria of certain dates
    By hobee in forum Excel General
    Replies: 2
    Last Post: 06-25-2012, 12:52 AM
  4. [SOLVED] Excel 2007 : How to get sum of a criteria within specified dates
    By devawad in forum Excel General
    Replies: 2
    Last Post: 04-23-2012, 04:52 AM
  5. Sum If between two dates and other criteria
    By whiZZfiZZ in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 03-02-2005, 09:07 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1