+ Reply to Thread
Results 1 to 8 of 8

Problem with defining arr for calculating Average and STDEV

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Problem with defining arr for calculating Average and STDEV

    I found an Excel VBA online that can calculate the average and the STDEV of numbers in Cells A1 to A10. See script below. This works. I tested it.
    What I would like to do now is change this script so that it can be used for 1D data arrays with varying amount of data.
    Assume I have data in column B. The first row is always row 2 but the last row varies.
    How would the script look like assuming that the size of the array is variable and the last row is variable?

    Function Mean(k As Long, Arr() As Single)
    
        Dim Sum As Single
        Dim i As Integer
        
        Sum = 0
        For i = 1 To k
            Sum = Sum + Arr(i)
        Next i
        
        Mean = Sum / k
    
    End Function
    Function StdDev(k As Long, Arr() As Single)
    
        Dim i As Integer
        Dim avg As Single, SumSq As Single
        
        avg = Mean(k, Arr)
        For i = 1 To k
             SumSq = SumSq + (Arr(i) - avg) ^ 2
        Next i
        
        StdDev = Sqr(SumSq / (k - 1))
    
    End Function
    Sub Compute()
    
        Dim Arr(10) As Single
        Dim Average As Single
        Dim Std_Dev As Single
        
        For i = 1 To 10
             Arr(i) = Sheets("Sheet1").Cells(i, 1)
        Next i
        
        Average = Mean(10, Arr)
        Std_Dev = StdDev(10, Arr)
        Sheets("Sheet1").Cells(5, 4) = Average
        Sheets("Sheet1").Cells(6, 4) = Std_Dev
    
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with defining arr for calculating Average and STDEV

    I found two other functions for average and STDEV. These functions seem better suited for what I want to do.
    Question: How does the Sub "Compute" need to be modified so that I can used it for a 1D array with varying last row?

    Function Mean(Arr)
    
        Dim Sum As Single
        Dim i As Integer
        Dim k1 As Long, k2 As Long
        Dim n As Long
        
        k1 = LBound(Arr)
        k2 = UBound(Arr)
        
        Sum = 0
        n = 0
        For i = k1 To k2
            n = n + 1
            Sum = Sum + Arr(i)
        Next i
        
        Mean = Sum / n
    
    End Function
    Function StdDev(Arr)
    
         Dim i As Integer
         Dim avg As Single, SumSq As Single
         Dim k1 As Long, k2 As Long
        
         Dim n As Long
        
         k1 = LBound(Arr)
         k2 = UBound(Arr)
        
         n = 0
         avg = Mean(Arr)
         For i = k1 To k2
            n = n + 1
              SumSq = SumSq + (Arr(i) - avg) ^ 2
         Next i
        
         StdDev = Sqr(SumSq / (n - 1))
    
    End Function
    Sub Compute()
    
        Dim Arr(10) As Single
        Dim Average As Single
        Dim Std_Dev As Single
        
        For i = 1 To 10
             Arr(i) = Sheets("Sheet1").Cells(i, 1)
        Next i
        
        Average = Mean(10, Arr)
        Std_Dev = StdDev(10, Arr)
        Sheets("Sheet1").Cells(9, 4) = Average
        Sheets("Sheet1").Cells(10, 4) = Std_Dev
    
    End Sub

  3. #3
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Problem with defining arr for calculating Average and STDEV

    You can use Excel builtin formulas for this, something like :

    Sub Compute_Range()
      Dim rng As Range
      With Sheets("Sheet1")
        Set rng = .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        Debug.Print rng.Address
    
        .Cells(9, 4).Value = Application.Average(rng)
        .Cells(10, 4).Value = Application.StDev(rng)
      End With
    End Sub
    
    Sub Compute_1D_Array()
      Dim Arr
      Arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
      Debug.Print "Average = " & Application.Average(Arr)
      Debug.Print "Stdev = " & Application.StDev(Arr)
    End Sub
    However if you want to use your existing UDF, the sub should be changed to something like this :
    Sub Compute()
        Dim rng As Range
        Dim Arr() As Single
        Dim Average As Single
        Dim Std_Dev As Single
    
        With Sheets("Sheet1")
          Set rng = .Range("A1:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
          Debug.Print rng.Address
          ReDim Arr(1 To rng.Rows.Count)
        End With
    
        For i = 1 To UBound(Arr)
             Arr(i) = rng.Cells(i)
        Next i
    
        Average = Mean(Arr)
        Std_Dev = StdDev(Arr)
        Sheets("Sheet1").Cells(9, 4) = Average
        Sheets("Sheet1").Cells(10, 4) = Std_Dev
    End Sub
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Problem with defining arr for calculating Average and STDEV

    For use of formulea in arrays take a look at this.
    http://www.snb-vba.eu/VBA_Arrays_en.html#L_6.16
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  5. #5
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with defining arr for calculating Average and STDEV

    Dear karedog, thank you for the solutions. I personally like the Compute_Range solution the best. This one I will use. But I also got your Sub Compute() script to work. I will keep this one in mind.
    bakerman2, thanks for the link.

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Problem with defining arr for calculating Average and STDEV

    You are welcome, glad this can help you. Thanks for marking the thread as solved.


    Regards

  7. #7
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    Re: Problem with defining arr for calculating Average and STDEV

    I wanted to add to your reputation but I was blocked from doing that. I have been given too many to you ...

  8. #8
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Problem with defining arr for calculating Average and STDEV

    Don't worry about that, thank you very much

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Problem with creating a correct formula for calculating STDEV for several time series.
    By Exceltrouble in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 03-14-2016, 09:43 AM
  2. [SOLVED] Problem calculating an average
    By mmccra2858 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-19-2013, 12:26 PM
  3. Problem Calculating an Average
    By Chrisb59 in forum Excel General
    Replies: 4
    Last Post: 08-01-2011, 05:41 AM
  4. Calculating stdev when values include #n/a
    By macaonghus in forum Excel General
    Replies: 2
    Last Post: 09-14-2009, 08:52 AM
  5. Calculating simple average and stdev
    By pani_hcu in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-11-2008, 11:29 AM
  6. average and stdev from groups of data
    By Charlie in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-09-2006, 12:00 AM
  7. STDEV without average
    By daniel in forum Excel General
    Replies: 5
    Last Post: 02-24-2005, 04:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1