Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default mean and variance

I want to calculate a mean and a variance, but when mean() is called
there is a type mismatch. why?

Function Mean(arr As Range)
Dim Sum As Single
Dim i As Integer

Sum = 0
For i = 1 To ran.Rows.Count
Sum = Sum + ran.Cells(1, i)
Next i

Mean = Sum / ran.Rows.Count
End Function

Function StdDev(arr As Range)
Dim i As Integer
Dim avg As Single, SumSq As Single

avg = Mean(arr)
For i = 1 To ran.Rows.Count
SumSq = SumSq + (ran.Cells(1, i) - avg) ^ 2
Next i

StdDev = Sqr(SumSq / (ran.Rows.Count - 1))
End Function

Sub MeanVar()

Dim ran As Range
Dim s, Mean, var, num As Integer


Set ran = Application.InputBox("in which range is the variable",
Type:=8)

M = Mean(ran)
s = StdDev(ran)

ran.Offset(1) = Mean
ran.Offset(2) = s

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 718
Default mean and variance

Your functions have an "arr" parameter, but you use undefined variable "ran"
instead.

HTH
--
AP

"Dirk" a écrit dans le message de
oups.com...
I want to calculate a mean and a variance, but when mean() is called
there is a type mismatch. why?

Function Mean(arr As Range)
Dim Sum As Single
Dim i As Integer

Sum = 0
For i = 1 To ran.Rows.Count
Sum = Sum + ran.Cells(1, i)
Next i

Mean = Sum / ran.Rows.Count
End Function

Function StdDev(arr As Range)
Dim i As Integer
Dim avg As Single, SumSq As Single

avg = Mean(arr)
For i = 1 To ran.Rows.Count
SumSq = SumSq + (ran.Cells(1, i) - avg) ^ 2
Next i

StdDev = Sqr(SumSq / (ran.Rows.Count - 1))
End Function

Sub MeanVar()

Dim ran As Range
Dim s, Mean, var, num As Integer


Set ran = Application.InputBox("in which range is the variable",
Type:=8)

M = Mean(ran)
s = StdDev(ran)

ran.Offset(1) = Mean
ran.Offset(2) = s

End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default mean and variance

Dirk,
First you declare MEAN as a variable when it also the name of
your function so change one or the other.

Other points:

Functions parameter is ARR but your loop processes RAN - change to ARR. I
also advice you change your SINGLE declarations to DOUBLE.

HTH

"Dirk" wrote:

I want to calculate a mean and a variance, but when mean() is called
there is a type mismatch. why?

Function Mean(arr As Range)
Dim Sum As Single
Dim i As Integer

Sum = 0
For i = 1 To ran.Rows.Count
Sum = Sum + ran.Cells(1, i)
Next i

Mean = Sum / ran.Rows.Count
End Function

Function StdDev(arr As Range)
Dim i As Integer
Dim avg As Single, SumSq As Single

avg = Mean(arr)
For i = 1 To ran.Rows.Count
SumSq = SumSq + (ran.Cells(1, i) - avg) ^ 2
Next i

StdDev = Sqr(SumSq / (ran.Rows.Count - 1))
End Function

Sub MeanVar()

Dim ran As Range
Dim s, Mean, var, num As Integer


Set ran = Application.InputBox("in which range is the variable",
Type:=8)

M = Mean(ran)
s = StdDev(ran)

ran.Offset(1) = Mean
ran.Offset(2) = s

End Sub


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
% Variance Abdul[_2_] Excel Worksheet Functions 3 November 9th 09 06:25 AM
Variance FreddieP Excel Discussion (Misc queries) 5 September 28th 06 01:41 PM
Pivot Tables - Variance and Variance % PJS Excel Discussion (Misc queries) 2 January 18th 06 03:12 AM
Pivot Tables - Variance and % Variance fields CraigS Excel Discussion (Misc queries) 5 January 6th 05 12:22 AM
% variance Abdul[_6_] Excel Programming 3 June 30th 04 01:58 PM


All times are GMT +1. The time now is 05:01 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"