XL5: DSUM/DAVERAGE Functions Fail with Computed Criteria

Last reviewed: February 2, 1998
Article ID: Q122666

The information in this article applies to:
  • Microsoft Excel for the Macintosh, version 5.0
  • Microsoft Excel for Windows NT, version 5.0

SYMPTOMS

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.

CAUSE

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.

WORKAROUND

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" (without the quotation marks) 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.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/supportnet/refguide/ 


 '-----------------------------------------------------------------------
 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:

  1. Select the cell in which you want the DSUM result to appear.

  2. On the Tools menu, click Macro.

  3. In the Macro Name/Reference list, click NewDSUM. Then, click Run.

  4. When you are prompted to enter a field name, enter the field name and click OK.

The correct DSUM() value should appear in the active cell. If you enter an invalid field name, an error message will appear in the cell instead.

To compute the DAVERAGE() function, use the NewDSUM subroutine to compute the DSUM value. Then, enter the following formula

   =<DSUMCell>/DCOUNT(Database,<FieldName>,Criteria)

where <DSUMCell> is the cell that contains the DSUM value and <FieldName> is the field name in the database.

STATUS

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.00 5.00a 5.00c
Keywords : xlformula kbcode
Version : WINDOWS:5.0; MACINTOSH:5.0
Platform : MACINTOSH WINDOWS
Issue type : kbbug
Solution Type : kbworkaround


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 2, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.