Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default example using DSUM worksheet function in a VBA function

Does anyone have sample code of using
WorksheetFunction.DSum(rDB, rColumn, rCriteria) in a VBA function?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default example using DSUM worksheet function in a VBA function

Hi,

MsgBox Application.WorksheetFunction.DSum(Range("A1:D8"), Range("C1"),
Range("G3:G4"))

with:
- the table (headers + data): A1:D8
- the sum field header in C1 (here 'Data')
- the criteria as G3 being the header name (here 'Description') and G4 the
filter value for that header (here 'Gasket')

returns the correct value in my case.
--
Regards,
Sébastien
<http://www.ondemandanalysis.com


"excelman" wrote:

Does anyone have sample code of using
WorksheetFunction.DSum(rDB, rColumn, rCriteria) in a VBA function?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default example using DSUM worksheet function in a VBA function

Hi,
What is the best way to dynamically change the criteria to sum all the
fields in the Data column in a loop
Thanks

"sebastienm" wrote:

Hi,

MsgBox Application.WorksheetFunction.DSum(Range("A1:D8"), Range("C1"),
Range("G3:G4"))

with:
- the table (headers + data): A1:D8
- the sum field header in C1 (here 'Data')
- the criteria as G3 being the header name (here 'Description') and G4 the
filter value for that header (here 'Gasket')

returns the correct value in my case.
--
Regards,
Sébastien
<http://www.ondemandanalysis.com


"excelman" wrote:

Does anyone have sample code of using
WorksheetFunction.DSum(rDB, rColumn, rCriteria) in a VBA function?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default example using DSUM worksheet function in a VBA function

Could you please give a few examples.

The DSUM function requires that you write the criteria table to the sheet.
Instead, you could use the SUMPRODUCT function.

Say you have:
- A2:A100 : data for field Gender
- B2:B100: data for field Age
- C2:C100: data for field Sales

To get the sum of Sales for males (="M") older than 30yr-old(30), you would
use the formula:
= SUMPRODUCT( (A2:A100="M") * (B2:B10030) * (C2:C100) )

Note that it also allow wildcard charactyers, ie A2:A100="*" would return
the sum for all genders.

In vba you would use:

Sub test()
'sum Sales for Males of age 30
MsgBox GetSum(Range("C2:C100"), "=""M""", "30")
End Sub

Function GetSum(ColToSum As Range, GenderCriteria As String, AgeCriteria As
String)
Dim s As String
s = "= SUMPRODUCT( (" _
& Application.Intersect(ColToSum.EntireRow, _
ColToSum.Parent.Range("A2").EntireColumn).Address _
& GenderCriteria & ") * (" _
& Application.Intersect(ColToSum.EntireRow, _
ColToSum.Parent.Range("B2").EntireColumn).Address _
& AgeCriteria & ") * (" _
& ColToSum.Address & ")) "
GetSum = Application.Evaluate(s)
End Function

Finally to get sevral SUM columns, using the GetSum function above, you
would do something like :
Sub test()
Dim i As Long, rgToSum As Range
Dim firstCol As String, lastCol As String, rowsToSUm As String
Dim GCriteria As String, ACriteria As String

'sum from col C to E
firstCol = "C"
lastCol = "E"
rowsToSUm = "2:100"
GCriteria = "=""M"""
ACriteria = "30"

For i = Asc(firstCol) To Asc(lastCol)
Set rgToSum = Application.Intersect(Range(rowsToSUm), Range(Chr(i) &
":" & Chr(i)))
MsgBox GetSum(rgToSum, GCriteria, ACriteria)
Next i
End Sub

--
Regards,
Sébastien
<http://www.ondemandanalysis.com


"excelman" wrote:

Hi,
What is the best way to dynamically change the criteria to sum all the
fields in the Data column in a loop
Thanks

"sebastienm" wrote:

Hi,

MsgBox Application.WorksheetFunction.DSum(Range("A1:D8"), Range("C1"),
Range("G3:G4"))

with:
- the table (headers + data): A1:D8
- the sum field header in C1 (here 'Data')
- the criteria as G3 being the header name (here 'Description') and G4 the
filter value for that header (here 'Gasket')

returns the correct value in my case.
--
Regards,
Sébastien
<http://www.ondemandanalysis.com


"excelman" wrote:

Does anyone have sample code of using
WorksheetFunction.DSum(rDB, rColumn, rCriteria) in a VBA function?

Reply
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
DSUM function Kimmer New Users to Excel 13 April 22nd 08 05:42 PM
DSUM function Kimmer New Users to Excel 4 April 19th 08 09:14 PM
Using the function DSUM Tom Excel Discussion (Misc queries) 7 September 26th 07 11:43 PM
DSUM function Debra Lisa Excel Worksheet Functions 8 August 29th 07 03:24 PM
DSum function Mark Excel Worksheet Functions 2 November 17th 04 02:47 PM


All times are GMT +1. The time now is 12:17 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"