Extract years between two dates

koolwaters

Active Member
Joined
May 16, 2007
Messages
403
Hi,

I need to extract the years between two dates.

For example:

Start Date 2010-09-07
End Date 2014-08-30

The number of years between the Start Date and End Date are four.

As part of my query result, I need to see only the years between these two dates. So I would see,

2011 2012 2013 2014

If I have
Start Date 2010-09-07
End Date 2013-08-30

I need to see 2011 2012 2013

I hope that someone can assist.

Thanks in advance.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I actually still need it in the forecast because it lets us know what funds we need to request from Management to cover the disbursements for the year.

The budget is allocated at the beginning of each fiscal.

We have a cut-off dates for applications so that we do not run into problems of not having enough money to cover the requests that we have.

Thanks again!
 
Upvote 0
Hi,

Another query for you.

I have now been asked to provide a summary of this data by the employee's Department.

I have created a query based on my ttblForecat called qryForecast. Because the years will be dynamic, I have used the asterisk to use all of the fields from that table in my query.

The challenge comes in providing totals for each year. So basically, if 3 employees are form the IT Department, I need to see the Department in the 1st column and then the total Amount Approved and each year for those 3 employees as opposed to 3 separate records.

I have been at it since last night but I cannot find a way to get this done since the years are dynamic.
 
Upvote 0
Hello agian,

I'm not sure if I understand what your asking.

This is clear:
The challenge comes in providing totals for each year. So basically, if 3 employees are form the IT Department, I need to see the Department in the 1st column and then the total Amount Approved

from here don't follow:
and each year for those 3 employees as opposed to 3 separate records.
 
Upvote 0
Ok. The Sheet below is the output generated by the code you provided.
Excel Workbook
ABCDEFGHIJK
1DepartmentApplicationIDEmployeeIDStartDateEndDateAmountApproved2009 - Amt in (US$)2010 - Amt in (US$)2011 - Amt in (US$)2012 - Amt in (US$)2013 - Amt in (US$)
2Human Resources1114-Sep-0930-Aug-13$ 8,000.002,000.002,000.002,000.002,000.00
3Corporate2201-Mar-1101-Apr-11$ 6,000.006,000.00
4Human Resources3101-Mar-1101-Apr-11$ 6,000.006,000.00
Sheet1
Excel 2007

This is the additional output which I also need.
Excel Workbook
ABCDEFG
7DepartmentAmountApproved2009 - Amt in (US$)2010 - Amt in (US$)2011 - Amt in (US$)2012 - Amt in (US$)2013 - Amt in (US$)
8Human Resources$ 14,000.002,000.008,000.002,000.002,000.00
9Corporate$ 6,000.006,000.00
Sheet1
Excel 2007

So, I need to have summaries by Department.

I hope you can follow.
 
Upvote 0
Hi Michelle,

dynamic tables require dynamic queries,

next code creates a new querydef. Read comments to adjust before you run the code

Code:
Public Sub CreateDynamicQuery()
'Create a querydefinition
'call each time you want to query the data
Dim dbs As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field
Dim qdf As DAO.QueryDef
Dim iFldCnt As Integer
Dim sSumFields() As String
Dim sSQL_Grouped As String
Const sGroupField As String = "EmployeeID" 'This should be Department, but I had this already in the table over here, change to correct field
Const sTblName As String = "ttblForeCast"
Const sTempQueryName As String = "qForeCast"
Set dbs = CurrentDb
Set tdf = dbs.TableDefs(sTblName)
iFldCnt = 0
'Loop fields of forecast table
    For Each fld In tdf.Fields
 
        ReDim Preserve sSumFields(iFldCnt)
 
        Select Case fld.Name
                Case sGroupField
                    sSumFields(iFldCnt) = "[" & fld.Name & "]"
                     iFldCnt = iFldCnt + 1
                Case "ApplicationID", "StartDate", "EndDate" ' , "EmployeeID"     'these are the fields to skip, you need to add the EmployeeID
                'do nothing
                Case Else
                    sSumFields(iFldCnt) = "Sum([" & fld.Name & "]) as [Total " & fld.Name & "]"
                    iFldCnt = iFldCnt + 1
        End Select
 
    Next fld
'Construct the sql statement
sSQL_Grouped = "Select " & Join(sSumFields, ", ") & " from " & sTblName & " Group By [" & sGroupField & "]"
'create dynamic querydef
'first delete the query if exists
    For Each qdf In dbs.QueryDefs
        If qdf.Name = sTempQueryName Then dbs.QueryDefs.Delete qdf.Name
    Next qdf
'this creates the querydef in your database
Set qdf = dbs.CreateQueryDef(sTempQueryName, sSQL_Grouped)
 
End Sub
 
Upvote 0
You can add the call in the last line of the sub that creates the temp table

Code:
        dbs.Execute sSQL
    .MoveNext
    Loop
End With
[COLOR=red][B]CreateDynamicQuery
[/B][/COLOR]End Sub

This way the query always lines up with the table.
 
Upvote 0
One little issue. My Employee IDs have leading zeroes so I changed the Data Type to text in the line of code below. However, when the table is created, the EmployeID field is a text field but the leading zeroes are no longer there.

Code:
sYearFlds = "ApplicationID Long, EmployeeID Text, StartDate DateTime, EndDate DateTime, AmountApproved Currency, " & sYearFlds
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top