XL5: DSUM/DAVERAGE Functions Fail with Computed Criteria |
In Microsoft Excel version 5.0 for the Macintosh and Microsoft Excel version 5.0 for Windows NT, the DSUM() and DAVERAGE() functions may return incorrect results if you are using computed criteria for your database.
When you use computed criteria, the DSUM() and DAVERAGE() functions return
only the first record in the database that matches the criteria.
Note that the DCOUNT() function works correctly even if you use computed
criteria.
This problem does not occur in Microsoft Excel versions 5.0 and 5.0c for
Windows.
To avoid this problem, you can use the following Visual Basic procedure. When you run the procedure, you are prompted to enter the name of the field in the database for which you would like to compute the DSUM(). For example, if your DSUM() formula would appear as follows
=DSUM(Database,"Bravo",Criteria)
then enter Bravo in the dialog box that
prompts you to enter a field name. Once you enter the field name, the
procedure computes the correct DSUM value and enters that value in the
active cell in the worksheet.
http://www.microsoft.com/support/supportnet/overview/overview.asp
'-----------------------------------------------------------------------
Option Explicit
Sub NewDSUM()
'Dimension variables.
Dim DestCell As Object, FieldName As String
Dim FieldOffset As Integer, DCOUNTTotal As Integer
Dim DSUMTotal As Variant, Counter As Integer
'Remember where the active cell is so that the computed DSUM can be
'returned there.
Set DestCell = ActiveCell
'Prompt for a field name.
FieldName = InputBox("Please enter a field name to DSUM:")
'If an error occurs, go to the error handling procedure.
On Error GoTo BadFieldName
'Compute which column in the database has the field name that was
'entered.
FieldOffset = Application.Match(FieldName, _
Range("Database").Resize(1, Range("Database").Columns.Count), 0) _
- 1
'Compute how many records in the database match the criteria.
DCOUNTTotal = Application.DCountA(Range("Database"), FieldName, _
Range("Criteria"))
'Initialize variables for the loop.
DSUMTotal = 0
Counter = 0
'Loop...
Do
'Find the next record in the database that matches the criteria.
Application.ExecuteExcel4Macro ("DATA.FIND.NEXT()")
'Add the value of the cell in the appropriate column to the
'DSUMTotal.
DSUMTotal = DSUMTotal + ActiveCell.Offset(0, FieldOffset).Value
'Increment the Counter.
Counter = Counter + 1
'Exit loop when all records have been processed.
Loop While DCOUNTTotal > Counter
'Insert the computed DSUM into the destination cell.
DestCell.Value = DSUMTotal
'Select the destination cell.
DestCell.Select
'Exit the subroutine now.
Exit Sub
'This section only runs if the field name that was entered is invalid.
BadFieldName:
DestCell.Value = "Invalid field name!"
DestCell.Select
End Sub
'-----------------------------------------------------------------------
To use this procedure, do the following:
=<DSUMCell>/DCOUNT(Database,<FieldName>,Criteria)where <DSUMCell> is the cell that contains the DSUM value and <FieldName> is the field name in the database.
Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. This problem was corrected in Microsoft Excel for the Macintosh, version 5.0a.
Additional query words: 5.00a 5.00c
Keywords : kbcode xlformula
Version : MACINTOSH:5.0; winnt:5.0
Platform : MACINTOSH winnt
Issue type : kbbug
Technology :
|
Last Reviewed: November 9, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |