+ Reply to Thread
Results 1 to 2 of 2

Date Range Question

Hybrid View

agroom Date Range Question 04-24-2007, 06:00 PM
agroom Would this work? It checks... 04-25-2007, 12:25 PM
  1. #1
    Registered User
    Join Date
    02-15-2007
    Posts
    9

    Date Range Question

    I'm writing a macro to find out if a given month/year falls within a date range.

    The spreadsheet has 2 columns (3 and 4) which hold a start date and stop date. The macro needs to cycle through each month of the year (over 3 years) and count the rows that the given month falls within the date range

    The row gets counted even if it's only for 1 day.
    Example, if start date is 1/31/06, it would still get counted when checking for rows in the month of January 2006, same goes for stop dates.

    This is the code I have now:

    Dim row, Month, Year, count As Integer
    Dim Date1, Date2 As Date
    Dim Completion As Date
    Date1 = #1/31/2004#
    Date2 = #1/1/2004#
    row = 2 'starts at 2 due to the header row
    Month = 1
    Year = 1
    count = 0
    
    Do While Date1 < Now()
    
        Do While Cells(row, 1) <> ""
            Completion = Cells(row, 4)
            If Cells(row, 4) = "" Then
                Completion = Now
            End If
            If Cells(row, 3) <= Date1 And Completion >= Date2 Then
                count = count + 1
            End If
            row = row + 1
        Loop
        
        Cells(Month + 6, Year + 7) = count
    
        count = 0
        row = 2
        Date1 = DateAdd("M", 1, Date1)
        Date2 = DateAdd("M", 1, Date2)
        
        If Month = 12 Then
            Month = 1
            Year = Year + 1
        Else
            Month = Month + 1
        End If
    
    Loop
    The stop date might be blank (if the job is still outstanding), so I just assigned it todays date since the macro only checks up through the previous month.

    The Month and Year variables are used to place the results in a table correctly.

    I'm wondering if I have my IF statement correct to validate if the date range falls within the given month. I'd try to validate it using just a specific month/year, but there's thousands of rows, so manual counting is not an option and if my IF statement is incorrect, then I can't write macro to check just a single month.

    Maybe there's an easier function that can do this?

    Anyone have any suggestions?

  2. #2
    Registered User
    Join Date
    02-15-2007
    Posts
    9
    Would this work? It checks all 3 senerios, if the start date is within that month or the month range is between the two dates, or the stop date is within the month. All three would be acceptable to count that line.

    If (Cells(row, 3) >= Date1 And Cells(row, 3) <= Date2) Or _
       (Date1 > Cells(row, 3) And Date2 < Completion) Or _
       (Completion >= Date1 And Completion <= Date2) Then
                
         count = count + 1
                
    End If
    I also changed Date1 to be the first day of the month and Date2 to the last day of the month.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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