Problème de if en VBA
Bonjour,
Je débute en VBA et j'essaie de faire un tri sur trois valeurs a,b,c avec comme idée l'algo suivant:
3 pavés principaux: a>b , a=b, a<b
Dans ces pavés je teste C avec l'égalité, s'il est au dessus de la plus haute valeur, en dessous de la plus basse et sinon il est entre les deux.
Je me doute que cet algo est lourd mais bon, pour l'instant c'est le fleuron de mes programmes.
Ces 3 cas sont matérialisés par la structure
IF (1er pavé)
ELSEIF (2ème pavé)
ELSE (3ème pavé)
A l'intérieur de ces cas je traite c pour le situer avec
un IF, ELSEIF,...,ELSE et je termine ce sous IF par un ENDIF.
Le problème à présent:
En lançant ma procédure, VBA me dit sur le "ElseIf a = b Then" : Else sans if.
Pourtant j'ai l'impression que ma construction est logique et je ne comprends pas vraiment le souçi.
Merci d'avance de m'aider si possible.
Ci-dessous le code:
Sub tri()
Dim a, b, c As Integer
a = InputBox("nb a")
b = InputBox("nb b")
c = InputBox("nb c")
If a > b Then
If c = a Then MsgBox "a=c>b"
ElseIf c = b Then MsgBox "c>b=a"
ElseIf c > a Then MsgBox "c>a>b"
ElseIf c < b Then MsgBox "a>b>c"
Else: MsgBox "a>c>b"
End If
ElseIf a = b Then
If c = a Then MsgBox "c=a=b"
ElseIf c > a Then MsgBox "c>a=b"
Else: MsgBox "a=b>c"
End If
Else
If c = b Then MsgBox "c=b>a"
ElseIf c = a Then MsgBox "b>a=c"
ElseIf c > b Then MsgBox "c>b>a"
ElseIf c < a Then MsgBox "b>a>c"
Else: MsgBox "b>c>a"
End If
End If
End Sub[/code]
Bonjour,
Je n'ai vraiment pas regardé ni tenté de comprendre ce que tu essaies de faire. J'ai simplement modifié la syntaxe et indenté ton code. Voici le code qui doit marcher syntaxiquement :
Sub tri()
Dim a, b, c As Integer
a = InputBox("nb a")
b = InputBox("nb b")
c = InputBox("nb c")
If a > b Then
If c = a Then
MsgBox "a=c>b"
ElseIf c = b Then
MsgBox "c>b=a"
ElseIf c > a Then
MsgBox "c>a>b"
ElseIf c < b Then
MsgBox "a>b>c"
Else: MsgBox "a>c>b"
End If
ElseIf a = b Then
If c = a Then
MsgBox "c=a=b"
ElseIf c > a Then
MsgBox "c>a=b"
Else: MsgBox "a=b>c"
End If
Else
If c = b Then
MsgBox "c=b>a"
ElseIf c = a Then
MsgBox "b>a=c"
ElseIf c > b Then
MsgBox "c>b>a"
ElseIf c < a Then
MsgBox "b>a>c"
Else: MsgBox "b>c>a"
End If
End If
End SubMerci, en effet ça marche.
Je vois que le code est identique mais que tu as modifié l'indentation.
L'indentation compte en VBA ?
Ma question est stupide, je la retire
Merci beaucoup !
Re,
L'indentation compte uniquement pour la lisibilité d'un code. Ce n'est pas grâce à l'indentation que ça a marché. Mais dans ton ancien code à chaque condition et après le mot clé Then tu mettais immédiatement l'instruction à exécuter . Dans ce cas précis, le code n'attend pas à avoir un
End If ou Else ou bien ElseIfPar exemple
If <condition> Then <instruction>se termine ainsi sans End If ni Else ni ElseIf, par contre la syntaxe est différent dans les cas suivants :
If <condition> Then
<instruction>
End Ifou bien
If <condition> Then
<instruction>
Else
<instruction>
End Ifou bien
If <condition> Then
<instruction>
ElseIf
<instruction>
ElseIf
<instruction>
End Ifou encore
If <condition> Then
<instruction>
ElseIf
<instruction>
ElseIf
<instruction>
Else
<instruction>
End If