Bonjour, je suis débutant en vba et je me retrouve confronté à un problème que je ne comprends pas, et sur lequel je n'ai trouvé aucune solution malgré de multiples essaie et recherches sur Internet, j'en viens donc à poster ce message sur le forum en espérant recevoir un peu d'aide


Voici la version du code qui me pose problème :



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
Option Explicit
 
Function CalculRecetteDepensePerso(ByRef Montant_TransactionObj As Object, ByRef Nature_Transaction As Object, Compte_Transaction As Object) As Single
 
    Dim targetRow As Integer
    Dim targetCol As Integer
    Dim Montant_Transaction As Single
    Dim sens As String
    Dim functionReturn As Single
 
    Montant_Transaction = Montant_TransactionObj.Value
    targetRow = Application.Caller.Row
 
    If Compte_Transaction.Value = "Perso" And Application.Caller.Column = 8 Then
        targetCol = 8
    ElseIf Compte_Transaction.Value = "Livret Bleu" And Application.Caller.Column = 9 Then
        targetCol = 9
    ElseIf Compte_Transaction.Value = "Eurocompte" And Application.Caller.Column = 10 Then
        targetCol = 10
    ElseIf Compte_Transaction.Value = "Livret Jeune" And Application.Caller.Column = 11 Then
        targetCol = 11
    Else
        Exit Function
    End If
 
    If Nature_Transaction.Value = "D" Then
        sens = "-"
    ElseIf Nature_Transaction.Value = "R" Then
        sens = "+"
    End If
 
    Call CalculRecetteDepensePersoProcedure(Montant_Transaction, sens, targetRow, targetCol, functionReturn)
 
    CalculRecetteDepensePerso = functionReturn
 
End Function
 
Sub CalculRecetteDepensePersoProcedure(ByRef Montant_Transaction As Single, ByRef sens As String, ByRef targetRow As Integer, ByRef targetCol As Integer, ByRef functionReturn As Single)
 
    Dim derVal As Single
    Dim i As Integer
 
    '        If Cells(targetRow - 1, targetCol).Value <> "" Then
    '            functionReturn = Evaluate(Replace(CSng(Cells(targetRow - 1, targetCol).Value) & sens & Montant_Transaction, ",", "."))
    '        ElseIf Cells(targetRow - 1, targetCol).Value = "" Then
    '            functionReturn = Evaluate(derVal & sens & Montant_Transaction)
    '        End If
 
    For i = 2 To Cells(1, targetCol).End(xlDown).Row
        If Cells(i, targetCol).Value <> "0" And Cells(i, targetCol).Value <> "" Then
            derVal = Cells(i, targetCol).Value
        End If
    Next
 
 
    functionReturn = Evaluate(Replace(derVal & sens & Montant_Transaction, ",", "."))
 
End Sub
Une fois la boucle for de la procédure paramétrer terminer, je repasse sur ma fonction à laquelle j'assigne la valeur de la variable functionReturn. Ce que je ne comprends pas, c'est que les valeurs renvoyées via la deuxième version de mon code (ci-dessous), sont strictement identiques et de même type, mais cette deuxième version ne créer pas de boucle infinie comme la version ci-dessus.

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
Option Explicit
 
Function CalculRecetteDepensePerso(ByRef Montant_TransactionObj As Object, ByRef Nature_Transaction As Object, Compte_Transaction As Object) As Single
 
    Dim targetRow As Integer
    Dim targetCol As Integer
    Dim Montant_Transaction As Single
    Dim sens As String
    Dim functionReturn As Single
 
    Montant_Transaction = Montant_TransactionObj.Value
    targetRow = Application.Caller.Row
 
    If Compte_Transaction.Value = "Perso" And Application.Caller.Column = 8 Then
        targetCol = 8
    ElseIf Compte_Transaction.Value = "Livret Bleu" And Application.Caller.Column = 9 Then
        targetCol = 9
    ElseIf Compte_Transaction.Value = "Eurocompte" And Application.Caller.Column = 10 Then
        targetCol = 10
    ElseIf Compte_Transaction.Value = "Livret Jeune" And Application.Caller.Column = 11 Then
        targetCol = 11
    Else
        Exit Function
    End If
 
    If Nature_Transaction.Value = "D" Then
        sens = "-"
    ElseIf Nature_Transaction.Value = "R" Then
        sens = "+"
    End If
 
    Call CalculRecetteDepensePersoProcedure(Montant_Transaction, sens, targetRow, targetCol, functionReturn)
 
    CalculRecetteDepensePerso = functionReturn
 
End Function
 
Sub CalculRecetteDepensePersoProcedure(ByRef Montant_Transaction As Single, ByRef sens As String, ByRef targetRow As Integer, ByRef targetCol As Integer, ByRef functionReturn As Single)
 
'    Dim derVal As Single
'    Dim i As Integer
 
            If Cells(targetRow - 1, targetCol).Value <> "" Then
                functionReturn = Evaluate(Replace(CSng(Cells(targetRow - 1, targetCol).Value) & sens & Montant_Transaction, ",", "."))
            'ElseIf Cells(targetRow - 1, targetCol).Value = "" Then
                'functionReturn = Evaluate(derVal & sens & Montant_Transaction)
            End If
 
'    For i = 2 To Cells(1, targetCol).End(xlDown).Row
'        If Cells(i, targetCol).Value <> "0" And Cells(i, targetCol).Value <> "" Then
'            derVal = Cells(i, targetCol).Value
'        End If
'    Next
'
'
'    functionReturn = Evaluate(Replace(derVal & sens & Montant_Transaction, ",", "."))
 
End Sub


J'ai fait des tas de tests, et j'ai la certitude que ça vient de cette boucle for, mais je ne vois pas ce qui cloche.