Excel VBA function returning #VALUE

wpennajr

New Member
Joined
Nov 29, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
HI all,

I'm new to this forum so forgive me if my question has been answered before, but I searched the FAQ and I didn't find an answer.

I'm creating an Excel function to perform a statistical test. Basically, the input is a range of cells containing my data set. Then I create an array with the data in the range, sort the array in ascending order and then perform the statistical calculations. I saved the code as an add-in. However I'm getting #VALUE when I try to use the function.

I checked all the calculation in a macro, comparing to calculations made in the spreadsheet and they are correct, so I assume it is some issue about how to create a custom function. I hope anyone can shed some light.

Thank you in advance.



VBA Code:
Function Normtest(sel As Range)
    Dim x() As Double, z() As Double, Sx() As Double, Fx() As Double, T1() As Double
    Dim n As Integer, i As Integer, j As Integer
    Dim Firstz As Double, Lastz As Double, strTemp As Double
    Dim AvgNorm As Double, StDevNorm As Double, h As Double
    Dim t As Double, dn As Double, Tcrit As Double
   
    'Read data and count the number of data points
    Set sel = Application.Selection
    n = sel.Rows.Count
   
    'Calculate the x-values array average and standard deviation
    AvgNorm = Application.WorksheetFunction.Average(sel)
    StDevNorm = Application.WorksheetFunction.StDev(sel)
   
    'Create the x-values array and the normalized x-values array
    ReDim x(1 To n)
    ReDim z(1 To n)
    For i = 1 To n
        x(i) = sel.Cells(i).Value
        z(i) = (x(i) - AvgNorm) / StDevNorm
    Next i
       
    'Sort the normalized x-values array in ascending order
    Firstz = LBound(z)
    Lastz = UBound(z)
    For i = Firstz To Lastz - 1
        For j = i + 1 To Lastz
            If z(i) > z(j) Then
                strTemp = z(i)
                z(i) = z(j)
                z(j) = strTemp
            End If
        Next j
    Next i
   
    'Calculate the Sx-values array, Fx-values array and the Liliefors statistical test value
    ReDim Sx(1 To n)
    ReDim Fx(1 To n)
    ReDim T1(1 To n)
    t = 0
    h = 1 / n
    For i = 1 To n
        If i = 1 Then Sx(i) = h Else Sx(i) = Sx(i - 1) + h
        Fx(i) = Application.WorksheetFunction.NormDist(z(i), 0, 1, True)
        'Calculate the Lilliefors test statistic value
        T1(i) = Abs(Fx(i) - Sx(i))
        If T1(i) > t Then t = T1(i)
    Next i
   
    'Calculate the Lilliefors statistical test critical value for alpha = 0.05
    dn = Sqr(n) - 0.01 + 0.83 / Sqr(n)
    Tcrit = 0.895 / dn                                  'need to check this calculation in the book
   
    'Returns the Lilliefors test statistical value
    Normtest = t
   
End Function
 
Last edited by a moderator:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi & welcome to MrExcel.

Try deleting this line
VBA Code:
Set sel = Application.Selection

since the variable "sel" is used as an argument to your function:


Book1
ABCDEF
1123456
2456789
3222222
4
5
60.661477
Sheet1
Cell Formulas
RangeFormula
A6A6=Normtest(A1:F3)
 
Upvote 0
It worked! Thanks GWteB, for the quick and precise answer.
Regards
 
Upvote 0
Glad to help and thanks for the feedback (y)
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top