+ Reply to Thread
Results 1 to 9 of 9

Can't calculate a threshold value through a Function and a Sorting procedure of an Array

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Can't calculate a threshold value through a Function and a Sorting procedure of an Array

    I am trying alternative methods for calculating a minimum threshold value (0.05) using a Function, a Sort procedure, and arrays (CoursAction)

    However, I get a message that an array is expected for the Var Function.

    Does anyone have a guess what could the problem be?

    See the attachment below.


    Option Explicit
    Option Base 1
    
    Sub Performance()
    Dim N As Long
    Dim EnsembleActions As Range
    Dim Nb_Actions As Integer
    Dim Action As Range
    Dim CoursAction As Range
    Dim i As Integer
    Dim SharpeRatio As Double
    Dim TauxRf As Double
    Dim RendsEcart As Double
    Dim NomAction As String
    Dim ValeursAction() As Variant
    Dim NB As Integer
    Dim alpha As Double
    Dim VarA As Double
    
        With Worksheets("Actions")
            Nb_Actions = .Cells(1, Columns.Count).End(xlToLeft).Column
        End With
    
        With Worksheets("Actions")
            Set EnsembleActions = .Range(.Cells(2, 1), .Cells(.Rows.Count, Nb_Actions).End(xlUp))
        End With
    
        For Each Action In EnsembleActions.Columns
            i = i + 1
            Set CoursAction = Action
    
            TauxRf = Worksheets("Performance").Cells(2, 2).Value
            RendsEcart = WorksheetFunction.StDev(CoursAction)
            
             NB = WorksheetFunction.Count(CoursAction)
         
           Dim alpha As Double
           Dim Var As Double
           
            alpha = Worksheets("Performance").Cells(3, 2).Value
            
            VarA = VarB(CoursAction, alpha)
            
        
            NomAction = Worksheets("Actions").Cells(1, i).Value
    
            With Worksheets("Performance")
                .Cells(4 + i, 1) = NomAction
             
                .Cells(4 + i, 2) = VarA
                
            End With
        Next Action
    
    End Sub
    
    Function VarB(Rendements As Range, A As Double)
        Dim N As Double
        Dim i As Integer
        Dim CoursAction As Double
        
        CoursAction = 0
        
        N = WorksheetFunction.Count(Rendements)
    
        For i = 1 To N
            CoursAction = CoursAction + Rendements(i)
        Next i
        
        'We sort the array
        Call Tri1(CoursAction)
        
        VarB = CoursAction(Int(N * A)) 'A ici correspond au Alpha
        
        
    End Function
    
    Sub Tri1(plaga As Variant)
    Dim ligne_Deb As Long
    Dim ligne_Fin As Long
    
        ligne_Deb = LBound(plaga)
        ligne_Fin = UBound(plaga)
    
        Dim i As Long, J As Long
        Dim tmp As Long
    
        For i = ligne_Deb To ligne_Fin - 1
            For J = ligne_Fin To i + 1 Step -1
                If plaga(J, 1) < plaga(J - 1, 1) Then
                    tmp = plaga(J, 1)
                    plaga(J, 1) = plaga(J - 1, 1)
                    plaga(J - 1, 1) = tmp
                End If
            Next J
        Next i
    
    End Sub
    Attached Files Attached Files
    Last edited by seigna; 05-05-2013 at 06:29 PM.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Can't calculate a threshold value through a Function and a Sorting procedure of an Arr

    I've had a look at the code and as far as I can see the problem is that Var isn't a VBA function, you can access it with Application.WorksheetFunction.Var.
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Can't calculate a threshold value through a Function and a Sorting procedure of an Arr

    I had a different code where I named it differently and I got the same error that it was expecting an array, or that the subscript was out of range..

  4. #4
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Can't calculate a threshold value through a Function and a Sorting procedure of an Arr

    What Var does is simply get the value from the array that corresponds to Int(N*A) with N=number of rows and A = 0.05

  5. #5
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Can't calculate a threshold value through a Function and a Sorting procedure of an Arr

    Mmmm... but Var is just supposed to be a function I define and perform on CoursAction

  6. #6
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,926

    Re: Can't calculate a threshold value through a Function and a Sorting procedure of an Arr

    Is this all in one module? I note that Sub Performance has a variable Var (double) dimensioned, then, a couple of statements later you call Var the function. I wonder if what you are seeing is a naming conflict, where VBA is having trouble resolving the double variable Var and the function Var. I would try changing the name for one of those to something unique, to avoid the potential conflict.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  7. #7
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Can't calculate a threshold value through a Function and a Sorting procedure of an Arr

    Ahhh wait, I can just rename them to Function VarB otherwise.. It shouldn't change much though. My original function is called VarHistoric (the one on the real file I am working on) and it gives the same errors..

    It is supposed to be in a module in the future. (tomorrow)

  8. #8
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Can't calculate a threshold value through a Function and a Sorting procedure of an Arr

    Let me explain in more detail what I am doing there:

    As you can see, I have a file with a few column of data. I am going to perform calculations on each Column of the data (cause they correspond to stock values).
    Here, specifically, I am going to
    1. Construct a function that:
    a/ Fills an array with range values
    b/Sort the data in the array through a bubble sort procedure Sub Tri1
    c/ Extract from the array the value that corresponds to the Int(N*A) value where A is a percentage specified in the sheet, and N is the number of rows in each column.

    Now, it returns two errors: Expected Array and Subscript out of range. I have modified the code into three different things but I have never changed the fact I used that specific bubble sort procedure and that I place the values in an array. Could the problem be coming from somewhere there?

  9. #9
    Forum Contributor
    Join Date
    04-07-2013
    Location
    France
    MS-Off Ver
    Excel 2011
    Posts
    166

    Re: Can't calculate a threshold value through a Function and a Sorting procedure of an Arr

    I also addressed this issue here. http://www.excelforum.com/excel-prog...nd-arrays.html

    The code is a bit different though it is supposed to do the same thing.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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