• Initiateur de la discussion Initiateur de la discussion Cougar
  • Date de début Date de début

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 !

Cougar

XLDnaute Impliqué
Bonsoir le forum,

Comment simplifier se code :

Do Until ActiveCell.Offset = ""
If ActiveCell(0, -1) = "0" Or "12" Then
ActiveCell.FormulaR1C1 = "500"
Else
If ActiveCell(0, -1) = "1" Or "10" Then
ActiveCell.FormulaR1C1 = "20"
Else
If ActiveCell(0, -1) = "2" Or "9" Then
ActiveCell.FormulaR1C1 = "10"
Else
If ActiveCell(0, -1) = "3" Or "8" Then
ActiveCell.FormulaR1C1 = "5"
Else
If ActiveCell(0, -1) = "11" Then
ActiveCell.FormulaR1C1 = "50"
Else
If ActiveCell(0, -1) = "13" Then
ActiveCell.FormulaR1C1 = "10000"
Else
If ActiveCell(0, -1) = "15" Or "16" Or "17" Or "18" Or "19" Or "20" Then
ActiveCell.FormulaR1C1 = "1000000"
End If
End If
End If
End If
End If
End If
End If
End If
Loop

Merci
 
Re : Comment simplifier

Bonjour Cougar 🙂,
Do Until ActiveCell.Offset = ""
If ActiveCell(0, -1) = "0" Or "12" Then
ActiveCell.FormulaR1C1 = "500"
Else
If ActiveCell(0, -1) = "1" Or "10" Then
ActiveCell.FormulaR1C1 = "20"
Else
If ActiveCell(0, -1) = "2" Or "9" Then
ActiveCell.FormulaR1C1 = "10"
Else
If ActiveCell(0, -1) = "3" Or "8" Then
ActiveCell.FormulaR1C1 = "5"
Else
If ActiveCell(0, -1) = "11" Then
ActiveCell.FormulaR1C1 = "50"
Else
If ActiveCell(0, -1) = "13" Then
ActiveCell.FormulaR1C1 = "10000"
Else
If ActiveCell(0, -1) = "15" Or "16" Or "17" Or "18" Or "19" Or "20" Then
ActiveCell.FormulaR1C1 = "1000000"
End If
End If
End If
End If
End If
End If
End If
End If
Loop
Déjà, ce code est faux. Le Or sépare 2 tests, donc
Code:
If ActiveCell.[COLOR=red]Offset[/COLOR](0, -1) = [COLOR=red]0[/COLOR] [COLOR=red]Or ActiveCell.Offset(0, -1) = 12[/COLOR] Then
de plus, les valeurs numérique ne se mettent pas entre cotes...
Ton premier Offset ne sert à rien, par contre, ActiveCell fait référence à la première cellule de la sélection, donc là il te manque un Offset...
Code:
Do Until ActiveCell = ""
Select Case ActiveCell.Offset(0, -1)
Case 0, 12
    ActiveCell.FormulaR1C1 = "500"
Case 1, 10
    ActiveCell.FormulaR1C1 = "20"
case2 , 9
    ActiveCell.FormulaR1C1 = "10"
Case 3, 8
    ActiveCell.FormulaR1C1 = "5"
Case 11
    ActiveCell.FormulaR1C1 = "50"
Case 13
    ActiveCell.FormulaR1C1 = "10000"
case15 , 16, 17, 18, 19, 20
    ActiveCell.FormulaR1C1 = "1000000"
Case Else
    MsgBox "Coucou"
End Select
Loop
Pour terminer, ce code va boucler à l'infini, vu que tu ne déplace jamais la sélection...
Bon courage 😎
Ajout : Salut Pierrot. ActiveCell(0,-1) est correct pour toi ?
 
Dernière édition:
Re : Comment simplifier

Bonjour Cougar,

regarde le code ci-dessous si il peut t'aider, à compléter...

A noter vois pas trop l'interet de ta boucle "Do loop", vu que la cellule active est toujours la même...

Code:
Sub test()
If ActiveCell(0, -1) = "0" Or ActiveCell(0, -1) = "12" Then
    ActiveCell.Value = "500"
    ElseIf ActiveCell(0, -1) = "1" Or ActiveCell(0, -1) = "10" Then ActiveCell.Value = "20"
    ElseIf ActiveCell(0, -1) = "2" Or ActiveCell(0, -1) = "9" Then ActiveCell.Value = "10"
    ElseIf ActiveCell(0, -1) = "3" Or ActiveCell(0, -1) = "8" Then ActiveCell.Value = "5"
    'etc...
Else
    'autre instruction, si les tests ci-dessus n'ont rien donnés...
End If
End Sub

Avec "ElseIf ", la série de tests s'arretera dès qu'une condition sera avérée...

bonne journée
@+

Edition : bonjour JNP
 
- 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

  • Question Question
Microsoft 365 Problème de date
Réponses
5
Affichages
358
Réponses
4
Affichages
732
Retour