[VBA E] Résult fonction instable si utilisée sur +de1feuille

brokengillou Messages postés 1 Date d'inscription jeudi 9 mars 2006 Statut Membre Dernière intervention 9 mars 2006 - 9 mars 2006 à 11:14
cs_eldim Messages postés 956 Date d'inscription lundi 30 mai 2005 Statut Membre Dernière intervention 21 août 2014 - 9 mars 2006 à 17:44
Bonjour,

J'ai réalisé une fonction qui renvoie le résultat attendu mais qui se
comporte bizarrement lorsque on l'insère dans plusieurs feuilles.

En fait le contexte de calcul de la feuille visualisée précédament
influe sur la feuille active qui renvoie des résultats abérants tant
que l'on ne fait pas F9.


Merci d'avance pour votre aide.





Cordialement,





brokengillou


' Fonction moyenne personnalisee prend en arguments cellule cible et colonne des coefficients.
' V 0.1 08/03/2006

'Function MOY_PERSO(Feu_Cal_A As String, Col_Coe_R As Range, Cel_Moy_R As Range)
Function MOY_PERSO(Col_Coe_R As Range, Cel_Moy_R As Range)

'Stop '' Permet le debug
'Stop

' Application.Volatile '' True par defaut Propriete force le recalcul de toute la fonction pour assurer le recalcul conformement à Microsoft KB463922
Application.Volatile

' expression.Volatile(parametre) '' Propriete expression = objet parametre True par defaut ou false. Force le recalcul dans tout le classeur même si pas de nouvelles valeurs seuls les param de la fonction sont verif.
'MOY_PERSO.Volatile

' Application.ScreenUpdating = True '' Propriete pour accelerer l'execution False en debut True à la fin
Application.ScreenUpdating = False

' Application.CalculateFullRebuild '' Methode pour forcer le recalcul dans tous les classeurs et reinitialiser les dependances.
' Application.CalculateFullRebuild

' Remplacement ActiveCell par Application.Caller Article Microsoft KB172760 fonctionne parfaitement sous cette version dans l'exemple donne
' ?? pas d ActiveCell

' Passage des plages en argument pour assurer le recalcul conformement à Microsoft KB463922

' Nom de l'onglet NomOnglet = Application.Caller.Parent.Name

Dim Cel_Moy_A As String

Dim Col_Moy_A As String
Dim Lig_Moy_N As Integer

Dim Col_Coe_A As String
Dim Col_Coe_N As Integer

Dim Ligv_N, Colv_N As Integer

Dim Lig_Vid_N As Integer

Dim Lig_DebSct_V As Variant
Dim Lig_FinSct_V As Variant

Dim Pla_Not_A As String
Dim Pla_Coe_A As String

Dim Som_Coe_N As Single

Dim Num_N As Single
Dim Den_N As Single

Dim Ssi_AE_N As Single
Dim Ssi_AR_N As Single
Dim Ssi_NU_N As Single

Cel_Moy_A = Cel_Moy_R.Address(0, 0)

Col_Moy_A = Left$(Cel_Moy_R.Address(0, 0), (Cel_Moy_R.Column < 27) + 2)
Lig_Moy_N = Cel_Moy_R.Row

Col_Coe_A = Left$(Col_Coe_R.Address(0, 0), (Col_Coe_R.Column < 27) + 2)
Col_Coe_N = Col_Coe_R.Column

Lig_Vid_N = Range("A65536").End(xlUp).Row + 1

    Ligv_N = 1
    Colv_N = 0
        
    Do
                If Cel_Moy_R.Offset(Ligv_N, Colv_N).Interior.PatternColor 16711935 Then Exit Do Else Ligv_N Ligv_N + 1
    
    Loop While Ligv_N < Lig_Vid_N

Lig_DebSct_V = Lig_Moy_N + 1
Lig_FinSct_V = Ligv_N - 1 + Lig_DebSct_V - 1

Pla_Not_A = Col_Moy_A &amp; Lig_DebSct_V &amp; ":" &amp; Col_Moy_A &amp; Lig_FinSct_V
Pla_Coe_A = Col_Coe_A &amp; Lig_DebSct_V &amp; ":" &amp; Col_Coe_A &amp; Lig_FinSct_V

Num_N = WorksheetFunction.SumProduct(Range(Pla_Not_A), Range(Pla_Coe_A))

Ssi_AE_N = WorksheetFunction.SumIf(Range(Pla_Not_A), "AE", Range(Pla_Coe_A))
Ssi_AR_N = WorksheetFunction.SumIf(Range(Pla_Not_A), "AR", Range(Pla_Coe_A))
Ssi_NU_N = WorksheetFunction.SumIf(Range(Pla_Not_A), "", Range(Pla_Coe_A))

Som_Coe_N = Cells(Lig_Moy_N, Col_Coe_N).Value

Den_N = Som_Coe_N - Ssi_AE_N - Ssi_AR_N - Ssi_NU_N
If Den_N 0 Then MOY_PERSO "--,--" Else MOY_PERSO = Num_N / Den_N

'Application.ScreenUpdating = True

End Function

1 réponse

cs_eldim Messages postés 956 Date d'inscription lundi 30 mai 2005 Statut Membre Dernière intervention 21 août 2014 1
9 mars 2006 à 17:44
Bonjour,

Au lieu de mettre "range" essai en précisant Worksheets("NomFeuille").Range

-- Pourquoi faire simple quand on peut faire compliquer --
0
Rejoignez-nous