Ceci est une page optimisée pour les mobiles. Cliquez sur ce texte pour afficher la vraie page.

XL 2010 Boucles - Délai de récupération - Finance

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

Olivier1978

XLDnaute Nouveau
Bonjour David et bonjour à tous,

Je travaille sur un fichier financier qui concerne le délai de récupération d'un investissement.

J'aurai besoin de faire deux boucles. J'ai essayé avec la fonction "si", la fonction "min" mais cela ne répond pas à mon besoin.
Pour "vous" cela va vous faire rire, mais moi je n'y arrive pas.

Je vous joins le fichier, j'ai tout expliqué dedans.

Le code que vous me proposerez, me permettra d'être "meilleur" sur les "boucles".

Je vous remercie pour votre aide.

Bon week-end à tous,

Olivier
 

Pièces jointes

Solution
Re,
"Just for the fun", avec une seule boucle :
VB:
Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Fin: If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("C3:C5,C14:G15")) Is Nothing Then
         Dim C%, InvestInit, PlusProche, NbJours%, Trouvé%
         Application.ScreenUpdating = False
         Application.EnableEvents = False
         InvestInit = [C5]: NbJours = [C3]: Trouvé = 0: PlusProche = 0
         For C = 3 To 7
            If Cells(15, C) >= InvestInit And Trouvé = 0 Then
                [L16] = Cells(14, C) / NbJours
                Trouvé = 1
            End If
            If Cells(15, C) <= InvestInit Then
                If InvestInit - Cells(15, C) > PlusProche Then PlusProche = Cells(15, C)...
Bonjour Olivier,
Un essai en PJ avec :
VB:
Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Fin: If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("C3:C5,C14:G15")) Is Nothing Then
         Dim C%, InvestInit, PlusProche
         Application.ScreenUpdating = False
         Application.EnableEvents = False
         InvestInit = [C5]: NbJours = [C3]
         For C = 3 To 7
            If Cells(15, C) >= InvestInit Then
                [L16] = Cells(14, C) / NbJours
                Exit For
            End If
         Next C
         PlusProche = 0
         For C = 3 To 7
            If Cells(15, C) <= InvestInit Then
                If InvestInit - Cells(15, C) > PlusProche Then PlusProche = Cells(15, C)
            End If
         Next C
         [L17] = InvestInit - PlusProche
    End If
Fin:
Application.EnableEvents = True
End Sub
 

Pièces jointes

Re,
"Just for the fun", avec une seule boucle :
VB:
Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Fin: If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("C3:C5,C14:G15")) Is Nothing Then
         Dim C%, InvestInit, PlusProche, NbJours%, Trouvé%
         Application.ScreenUpdating = False
         Application.EnableEvents = False
         InvestInit = [C5]: NbJours = [C3]: Trouvé = 0: PlusProche = 0
         For C = 3 To 7
            If Cells(15, C) >= InvestInit And Trouvé = 0 Then
                [L16] = Cells(14, C) / NbJours
                Trouvé = 1
            End If
            If Cells(15, C) <= InvestInit Then
                If InvestInit - Cells(15, C) > PlusProche Then PlusProche = Cells(15, C)
            End If
         Next C
         [L17] = InvestInit - PlusProche
    End If
Fin:
Application.EnableEvents = True
End Sub
 

Pièces jointes

- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Les cookies sont requis pour utiliser ce site. Vous devez les accepter pour continuer à utiliser le site. En savoir plus…