XL: Order Data Is Entered Changes Results of STDEV() Function

ID: Q111727


The information in this article applies to:
  • Microsoft Excel for Windows, version 5.0
  • Microsoft Excel for Windows 95, version 7.0


SYMPTOMS

In Microsoft Excel, the results you get when you use the STDEV() function may vary depending on the order in which you enter the values. This behavior is incorrect: the results of this function should not depend on the order in which you enter the values.


CAUSE

The process used to calculate the standard deviation involves the subtraction of two values that may be very similar to each other. The numbers may be calculated incorrectly because the two numbers may be rounded incorrectly. This incorrect rounding occurs because of the way a computer stores and manipulates numbers.


WORKAROUND

To work around this problem, round the data when you calculate the standard deviation.

The following is a sample Visual Basic function that you can use to round the data as you calculate the standard deviation.

Microsoft provides examples of Visual Basic for Applications procedures 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. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support professionals can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Visual Basic Example

  1. In a new Visual Basic module, enter the following macro code:
    
       'Rounded_Stdev function to calculate the rounded standard deviation
    
       ' Define the Rounded_StDev function with a data type of Double.
       ' This data type is consistent with type that Microsoft Excel uses.
    
       Function Rounded_Stdev(x_values As Variant) As Double
    
         'Dimension memory for variables
         Dim n As Integer
         Dim sum_of_x As Double, sum_of_x_squared As Double
    
         'Initialize variables before For Each loop
         sum_of_x = 0
         sum_of_x_squared = 0
    
         'calculates count of x_values
         n = x_values.Count
    
         For Each cell In x_values
            sum_of_x = sum_of_x + cell.Value
            sum_of_x_squared = sum_of_x_squared + cell.Value ^ 2
         Next
    
         'Perform math necessary to calculate standard deviation.
         Rounded_Stdev = n * sum_of_x_squared - sum_of_x ^ 2
    
         'Round at this step to remove any possible mathematical discrepancies.
         Rounded_Stdev = Application.Round(Rounded_Stdev, 14)
    
         'Continue to perform math necessary to calculate standard deviation.
         Rounded_Stdev = Rounded_Stdev / (n * (n - 1))
         Rounded_Stdev = Rounded_Stdev ^ 0.5
    
       End Function 


  2. Select a cell in the worksheet on which you want the results of the STDEV() function to appear, and choose the Function Wizard button.


  3. In the Function Wizard dialog box, under Function Category, select the User Defined category. Under function name, select Rounded_Stdev, and then choose the Next button.


  4. In the Function Wizard--Step 2 of 2 dialog box, in the x_values box, enter the range that contains the values for which you want to calculate the standard deviation (you can enter these values by selecting them in the worksheet).


  5. Choose the Finish button.



MORE INFORMATION

For more information about Data Types, choose the Search button in Visual Basic Help, and type the following:

data types

Additional query words:

Keywords : kbprg
Version : WINDOWS:5.0,7.0
Platform : WINDOWS
Issue type :


Last Reviewed: October 25, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.