LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Function for summing values of a field by date criteria

If your going to use worksheet functions then

=sumif(A:A,"=2004/01",B:B)-sumif(A:A,"2004/03",B:B)

would work in a cell, so you can use the same thing in your function.

Function HistoricTotal(text1 As String, text2 As String) As Long
Dim Tot as Long
with Application
tot = .sumif(Range("A:A"),"=" & Text1, Range("B:B"))- _
.sumif(Range("A:A"),"" & text2, Range("B:B"))
End With
HistoricTotal = Tot
End Function

I haven't tested it with your unusual date format, but assume it won't be
misinterpreted as division, or if it is, that it will still work.


--
Regards,
Tom Ogilvy


"Lawrence" wrote in message
om...
Hi All

Just started using VB(A). I just finished writing this function that
works fine but was interested in getting some feedback on how I could
improve it or other methods of approaching the problem.

Requirement:
To be able to sum values of a table/list (in Excel) within a given
period.

e.g. for Table 1:

Somefunction(2004/01, 2004/03) = 18


Example of tables:

Table 1:

Column A Column B

Date CountOfRecords
======= ==============
2004/01 10
2004/02 5
2004/03 3
2004/06 9


Table 2:

Date CountOfRecords
======= ==============
2003/01 1
2003/03 5
3
2003/06 11




VBA code:

================================================== =================
Function HistoricTotal(text1 As String, text2 As String) As Integer

Dim rng As Range
Dim Counter As Integer

Counter = 0

Set rng = Sheets("Sheet1").Range("A:B")

If text1 = text2 Then
HistoricTotal = Application.VLookup(text1, rng, 2, False)
Else
While text1 <= text2
On Error Resume Next
Counter = Counter + Application.VLookup(text1, rng, 2, False)

text1 = Application.Text((DateAdd("m", 1, text1)), "yyyy/mm")

Wend

HistoricTotal = Counter
End If

End Function
================================================== ===================


Thanks

Lawrence



 
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
Summing days that match a criteria and date Saylindara Excel Discussion (Misc queries) 3 December 15th 09 08:50 PM
Summing data between two date criteria Anna Wood Excel Worksheet Functions 5 February 27th 09 10:25 PM
Summing values in a database against certain criteria Bella Gray Excel Worksheet Functions 1 June 2nd 08 12:32 PM
summing values in a data table based on criteria in another column Dave F Excel Worksheet Functions 7 August 26th 06 04:36 PM
V-lookup and summing values if more than 1 matches criteria holcay Excel Worksheet Functions 3 February 6th 06 03:52 PM


All times are GMT +1. The time now is 10:09 PM.

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"