IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

VBA -Calculer le rendement et mettre résultats dans une autre feuille


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre habitué
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2015
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2015
    Messages : 11
    Par défaut VBA -Calculer le rendement et mettre résultats dans une autre feuille
    Bonjour,

    Voilà, j'essai de réaliser ceci avec VBA mais je n'y arrive pas

    J'essaie de créer une sub procedure (ProcReturns) qui utilisera une user-functtion (fnReturns): ProcReturn doit utiliser la fonction fnReturns pour calculer les valeurs d'une table (des actions ordonnées dans 3 colonnes X,Y,Z) dans une autre feuille de calcules nommées ex2_result. Ce calcul doit s'effectuer en prenant la valeur suivante (dans la même colonne) puis la diviser par la valeur précédente (toujours de la même colonne) et enfin soustraire 1 et cela pour toute les valeur de la table. Fromule exemple: B2/B1 - 1.

    Voilà ce que j'ai déjà réalisé mais les résultat ne s'affiche pas dans l'autre feuille de calcul pourtant le macro s'opère correctement ! :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    Option Base 1
    Option Explicit
     
    Function fnReturns(stockPrice As Variant) As Variant
     
     
    'variables
    Dim Returns() As Variant 'matrix that will contain the returns for the three stocks (result of the function fnReturns)
     
    Dim n_returns As Integer 'nb of returns to calculate for each stock
    Dim n As Integer 'nb of rows of the table in wks ex2_data
    Dim m As Integer 'nb of columns of the table in wks ex2_data
    Dim i As Integer 'loop variable
    Dim j As Integer 'loop variable
     
    'get the nb of rows and columns of the table in wks ex2_data and assign the values to the variables n and m
     
    n = Worksheets("ex2_data").Range("B2:D13").Rows.Count
     
    m = Worksheets("ex2_data").Range("B2:D13").Columns.Count
     
    'assign to the variable n_returns the nb of returns to calculate using the variable n
     
    n_returns = (n * m) - 3
     
    'Resize the matrix Returns taking into account the nb of returns to calculate for the three stocks
     
    ReDim Returns(n_returns) As Variant
     
    ' Calculate the returns in the matrix Returns for the three different stocks :
    ' - You need to use two loops for..Next
    '- one for the rows and one for the columns of your matrix
    'recall: to compute a return with divide a price with the previous one and substract 1 (returns(t)=price(t)/price(t-1)-1)
     
     
    For i = 2 To n
     
    For j = 1 To m - 1
     
    Returns(i - 1) = (stockPrice(i, j) / stockPrice(i - 1, j)) - 1
     
    Next j
     
    Next i
     
     
     
    'Return the matrix Returns
     
    Returns = stockprice
     
    End Function
     
    ------------------------------------------
     
    Sub procReturns()
     
     
    'wks variables
    Dim wsD As Worksheet 'wks variable for the wks named ex2_data
    Dim wsR As Worksheet 'wks variable for the wks named ex2_results
     
    'variant variables
    Dim stockPrice As Variant 'contains the stock prices of the three stocks (input of the function fnResults)
    Dim fn_result As Variant ' contains the result of the function fnReturns (output)
     
    Dim outputReturns As Variant
     
    'numerical variables
    Dim nb_rows As Integer
    Dim bn_col As Integer
     
     
    'Assign the worksheets to the wks variables
    Set wsD = Worksheets("ex2_data")
    Set wsR = Worksheets("ex2_results")
     
     
    'assign the nb of rows and columns of the input table to the variable nb_row and nb_col
    nb_rows = wsD.Range("B2:D13").Rows.Count
    bn_col = wsD.Range("B2:D13").Columns.Count
     
     
    'Assign the values of the range of stock prices to the variable stockPrice
    'Hint: resize a range using nb_rows and nb_col in order to get the range of cells then apply _
    the property value to assign the values of the range to the variable stockPrice
     
    ReDim stockPrice(nb_rows, bn_col)
     
    stockPrice = wsD.Range("B2:D13").Value
     
    'Assign to the variable outputReturns the value of the range of cells colored in orange in the wks "ex2_results"
    'Hint: use again the property resize and the property value
     
    ReDim outputReturns(nb_rows, bn_col)
    outputReturns = wsR.Range("B2:D12").Value
     
     
    'Assign to the variable fn_result the value returned by the function fnReturns 
    'with the variable StockPrice as the argument of the function (the variable StockPrice cannot be a range object!)
     
    fn_result = fnReturns(stockPrice)
     
    outputReturns = fn_result
     
    End Sub
    Fichiers attachés Fichiers attachés

  2. #2
    Expert éminent


    Profil pro
    Inscrit en
    Juin 2003
    Messages
    14 008
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 14 008
    Par défaut
    Bonjour ,


    il semble manquer l'écriture des données vers feuille résultat :


    Peu-être :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
     
    '(...)
    ReDim outputReturns(nb_rows, bn_col)
    outputReturns = wsR.Range("B2:D12").Value '  ???????????? A quoi cela sert ???
     
     
    'Assign to the variable fn_result the value returned by the function fnReturns _
    'with the variable StockPrice as the argument of the function (the variable StockPrice cannot be a range object!)
     
    fn_result = fnReturns(stockPrice)
     
    outputReturns = fn_result
     
    'A rajouter : ---------------------------------------------
     wsR.Range("B2:D12").Value = outputReturns
    '----------------------------------------------------------------------
    End Sub

  3. #3
    Membre habitué
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2015
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2015
    Messages : 11
    Par défaut
    Bonsoir bbil,

    En rajoutant outputReturns = wsR.Range("B2:D12").Value cela ne fonctionne toujours pas rien ne s'affiche :-/
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    '(...)
    ReDim outputReturns(nb_rows, bn_col)
    outputReturns = wsR.Range("B2:D12").Value ' --> C'est pour que les valeurs de outputReturns s'affiche dans la plage "B212" non?
     
     
    'Assign to the variable fn_result the value returned by the function fnReturns _
    'with the variable StockPrice as the argument of the function (the variable StockPrice cannot be a range object!)
     
    fn_result = fnReturns(stockPrice)
     
    outputReturns = fn_result
     
    'A rajouter : ---------------------------------------------
    wsR.Range("B2:D12").Value = outputReturns
    '----------------------------------------------------------------------
    End Sub

  4. #4
    Expert éminent


    Profil pro
    Inscrit en
    Juin 2003
    Messages
    14 008
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 14 008
    Par défaut
    Citation Envoyé par chou2best Voir le message
    Bonsoir bbil,

    En rajoutant "outputReturns = wsR.Range("B212").Value" cela ne fonctionne toujours pas rien ne s'affiche :-/
    ...
    ce n'est pas cette ligne que j'ai rajouté ... la copie est dans le mauvais sens ...!

  5. #5
    Membre habitué
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2015
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2015
    Messages : 11
    Par défaut
    Oui c'est vrais (erreur de copier/coller) mais même dans le sens que vous avez donnez ça ne m'affiche rien...pourtant ça me calcul bien les résultat (je la sais grâce à un espion).
    Du coup, c'est vachement frustrant parce qu'il me reste juste à afficher le résultat dans la bonne plage :-/

  6. #6
    Membre habitué
    Homme Profil pro
    Étudiant
    Inscrit en
    Octobre 2015
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2015
    Messages : 11
    Par défaut Update
    Désormais des résultats s'affiche dans la bonne table mais pas dans les bonnes cellules :-/

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    Option Base 1
    Option Explicit
     
    Function fnReturns(stockPrice As Variant) As Variant
     
     
    'variables
    Dim Returns() As Variant   'matrix that will contain the returns for the three stocks (result of the function fnReturns)
     
    Dim n_returns As Integer 'nb of returns to calculate for each stock
    Dim n As Integer  'nb of rows of the table in wks ex2_data
    Dim m As Integer  'nb of columns of the table in wks ex2_data
    Dim i As Integer  'loop variable
    Dim j As Integer   'loop variable
    Dim a As Integer
     
     
    'get the nb of rows and columns of the table in wks ex2_data and assign the values to the variables n and m
     
    n = Worksheets("ex2_data").Range("B2:D13").Rows.Count
     
    m = Worksheets("ex2_data").Range("B2:D13").Columns.Count
     
    'assign to the variable n_returns the nb of returns to calculate using the variable n
     
    n_returns = (n * m) - 3
     
    'Resize the matrix Returns taking into account the nb of returns to calculate for the three stocks
     
    ReDim Returns(n_returns) As Variant
     
     
     
    ' Calculate the returns in the matrix Returns for the three different stocks :
    ' - You need to use two loops for..Next
    '- one for the rows and one for the columns of your matrix
    'recall: to compute a return with divide a price with the previous one and substract 1 (returns(t)=price(t)/price(t-1)-1)
     
     
     
    For i = 2 To n
     
    For j = 1 To m
     
     
    Returns(i) = stockPrice(i, j) / stockPrice(i - 1, j) - 1
     
     
          Next j
     
    Next i
     
    'Return the matrix Return
    fnReturns = Returns
     
     
    End Function
     
    '------------------------
     
    Sub procReturns()
     
     
    'wks variables
    Dim wsD As Worksheet    'wks variable for the wks named ex2_data
    Dim wsR As Worksheet    'wks variable for the wks named ex2_results
     
    'variant variables
    Dim stockPrice As Variant    'contains the stock prices of the three stocks (input of the function fnResults)
    Dim fn_result As Variant     ' contains the result of the function fnReturns (output)
    Dim outputReturns As Variant
     
    'numerical variables
     
    Dim nb_rows As Integer
    Dim bn_col As Integer
     
     
    'Assign the worksheets to the wks variables
    Set wsD = Worksheets("ex2_data")
    Set wsR = Worksheets("ex2_results")
     
     
    'assign the nb of rows and columns of the input table to the variable nb_row and nb_col
    nb_rows = wsD.Range("B2:D13").Rows.Count
    bn_col = wsD.Range("B2:D13").Columns.Count
     
     
    'Assign the values of the range of stock prices to the variable stockPrice
    'Hint: resize a range using nb_rows and nb_col in order to get the range of cells then apply _
    the property value to assign the values of the range to the variable stockPrice
     
    ReDim stockPrice(nb_rows, bn_col)
     
    stockPrice = wsD.Range("B2:D13").Value
     
    'Assign to the variable outputReturns the value of the range of cells colored in orange in the wks "ex2_results"
    'Hint: use again the property resize and the property value
     
    ReDim outputReturns(nb_rows - 1, bn_col)
    outputReturns = wsR.Range("B2:D12").Value
     
     
     
    'Assign to the variable fn_result the value returned by the function fnReturns _
    with the variable StockPrice as the argument of the function (the variable StockPrice cannot be a range object!)
     
     
    fn_result = fnReturns(stockPrice)
     
     
    outputReturns = fn_result
    wsR.Range("B2:D12").Value = outputReturns
     
     
     
    End Sub

Discussions similaires

  1. Réponses: 12
    Dernier message: 20/05/2015, 17h15
  2. [XL-2010] Recherche multicritères et copie des résultats dans une autre feuille
    Par chacha49 dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 24/08/2011, 22h25
  3. [VBA-E] plage de reference dans une autre feuille
    Par gruget dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 02/09/2009, 13h28
  4. Concatener et afficher le résultat dans une autre feuille ?!
    Par pat212008 dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 24/11/2008, 11h56
  5. [VBA-E]Copier une serie de cellules dans une autre feuille
    Par Tartenpion dans le forum Macros et VBA Excel
    Réponses: 5
    Dernier message: 10/03/2006, 17h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo