Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting Dates in Excel. Harder than I thought. 1

Status
Not open for further replies.

aexley

Technical User
Jul 9, 2001
147
GB
I'm trying to count the number of records that fall in each month. I've tried DLookup, Count and CountIf but I must be getting something fundamental wrong as I can only get Excel to count none or all of the records. I realise that there is probably a very simple solution to this problem but I'll be damned if I can work it out.

I'm currently trying: -

=COUNTIF('SOURCE WORKSHEET'!B2:B750, AND(>=&quot;01/04/01&quot;, <=&quot;31/04/01&quot;))

But I don't get anything. I'm reaching the end of my tether here.

Thanks in advance for any help you can give.
 
I replaced the link of the 'SOURCE WORKSHEET' with 'A' for simplicity.

I couldn't tell for sure from your posting what kind of dates you were working with. I assumed April but then noticed 31 days?

If you wish to pick begin and end dates :
{=SUM((A!B2:B750>VALUE(&quot;01/03/2001&quot;))*(A!B2:B750<VALUE(&quot;02/04/2001&quot;))*COUNTA(A!B2:B750))/COUNTA(A!B2:B750)}

If you just want a single month, the math is easier :
{=SUM((MONTH(A!B2:B750)=4)*COUNTA(A!B2:B750))/COUNTA(A!B2:B750)}

Both of these formulas must be entered with Ctrl+Shift+Enter for the matrix array to work. Do not type the {} brackets, they will be added when the Ctrl+Shift+Enter keys are pressed.

Hope this helps.
 
Thanks JVFrederick the 'single month' formula has worked a charm. Oh, the 31 day April was just me being slack.

Thanks again.
 
Hi Aexley,

In your initial question, you mentioned trying the &quot;DLookup&quot; function. This caused me to believe you might be working with an Excel database table.

If indeed you might be working with such (or might want to create) an Excel database, you should be aware of the tremendous flexibility of the various database functions, including &quot;DCOUNT&quot;. Using this Excel database functionality allows for creating very powerful search &quot;criteria&quot;, where you could easily MIX your search criteria to include Employees, Addresses, Ages, etc TOGETHER with Dates.

The three parts of the &quot;Database Formula&quot; are: 1) Database, 2) Offset, and 3) Criteria. While too many Excel users still do NOT use &quot;Range Names&quot;, use of these names is HIGHLY recommended - because Excel then automatically takes care of adjusting the coordinates of the range names, as opposed to having to manually modify code or formulas whenever a row or column is inserted.

EXAMPLE Database Formula:
=DCOUNT(Database,Offset,Crit)
...wherein you assign the &quot;Database&quot; range name to your database;

&quot;Offset&quot; - replace this with the Field Name of the column you are performing your DCOUNT on (or DSUM, DMAX etc). Alternatively, you could (if appropriate for the context of your particular application) create a Range Name for the &quot;Offset&quot; portion of the Database formula. Your choice of names is arbitrary ...i.e. you can name is whatever you want ...I have used the name &quot;offset&quot; but you could call it any other name (except for a small number of &quot;reserved&quot; names). Then, in the cell which you have named &quot;offset&quot;, enter the field name of the column you wish to perform your DCOUNT or DSUM, etc.

The third portion of the Database Formula is &quot;Crit&quot; (This is short for &quot;Criteria&quot; which is a &quot;reserved&quot; name in Excel - see &quot;NOTE: RESERVED NAMES&quot; further below.) This again is an arbitrary Range Name. Whatever name you use in the database formula, assign that name to the range which contains the field name(s) and row(s) you have created for your set of search criteria. This is where for example you could use &quot;Employee&quot;, &quot;Age&quot;, etc as additional field names together with the &quot;Date&quot; field.

In the case of the simple MONTH formula which doesn't take into account the YEAR, you need to appreciate that this will ONLY be a valid option IF your database ONLY includes data for ONE year. If this IS the case, then the formula to use (in the row below the &quot;Date&quot; field in your criteria range) would be: =MONTH(Date)=4 for the month of April.

If you wanted to create a &quot;matrix&quot; of formulas for all months, you would then create separate criteria for each of the months. Create unique range names for each of the separate criteria ...example: Crit_Jan, Crit_Feb, Crit_Mar, Crit_Apr, etc. Then simply copy your first database formula and change the last three characters of the range name (for the subsequent months).

In case you are not aware, Excel's database functionality ALSO includes the ability to EXTRACT data to SEPARATE ranges and/or to SEPARATE Sheets.

Here is some VB code which is used to extract data:

Sub Extract_Data()
Application.ScreenUpdating = False
Range(&quot;database&quot;).AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=&quot;crit&quot;, _
CopyToRange:=Range(&quot;extr&quot;), _
Unique:=False
Application.ScreenUpdating = False
End Sub

NOTE: RESERVED NAMES: In Excel, as with other software, there are a few &quot;reserved&quot; range names. In this particular area (database formulas), there are two which you should NOT use: &quot;Criteria&quot; and &quot;Extract&quot;.

In case you want to create (now or in the future) database formulas which reference a database with MULTIPLE YEARS, you should use this formula in the cell below the &quot;Date&quot; field of your criteria:
=AND(Date>=From,Date<=To) ...where &quot;Date&quot; refers to the field name &quot;Date&quot;, &quot;From&quot; refers to a cell named &quot;From&quot;, in which you enter the &quot;from date&quot; (e.g. 04/01/2000), and &quot;To&quot; refers to a cell named &quot;To&quot; in which you enter the &quot;to date&quot; (e.g. 04/30/2000).

In creating a &quot;dynamic&quot; application, one should consider creating &quot;dynamic&quot; formulas for the &quot;From&quot; and &quot;To&quot; dates. These formulas would include the Month and Day as &quot;hard code&quot;, but reference a separate cell for the YEAR. This way, the end-user would only need to change that one cell which contains the YEAR (when advancing to the next year).

I hope this has been helpful.

Regards, ...Dale Watson dwatson@bsi.gov.mb.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top