VBA - IF, ELSEIF, AND, And Not, ELSE

debidebo67

XLDnaute Nouveau
Bonjour,

l'écriture du code VBA est accepté mais les résultats ne sont pas les bons, pourtant j'ai beau relire ça me parait juste. c'est comme s'il ne prend pas en compte certains ElseIf !?!
comme ça ne marche pas je pense que la manière de l'écrire n'est pas la bonne !

pourriez-vous m'aider SVP ?


Sub essaimacroboucle()

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Dim i As Integer
Worksheets("tableau 1").Range("P5:p1000").ClearContents


For i = 5 To 1000

'tous remli
If Cells(i, 6) & Cells(i, 7) & Cells(i, 8) & Cells(i, 9) = "" Then
Cells(i, 16).Value = ""


'manque 1
ElseIf (Cells(i, 8) = "") And Not (Cells(i, 6) & Cells(i, 7) & Cells(i, 9) = "") Then
Cells(i, 16).Value = Cells(i, 6) & " - " & Cells(i, 7) & " - " & Cells(i, 9)

ElseIf (Cells(i, 7) = "") And Not (Cells(i, 6) & Cells(i, 8) & Cells(i, 9) = "") Then
Cells(i, 16).Value = Cells(i, 6) & " - " & Cells(i, 8) & " - " & Cells(i, 9)

ElseIf (Cells(i, 9) = "") And Not (Cells(i, 6) & Cells(i, 7) & Cells(i, 8) = "") Then
Cells(i, 16).Value = Cells(i, 6) & " - " & Cells(i, 7) & " - " & Cells(i, 8)

'manque 2
ElseIf (Cells(i, 8) = "") And (Cells(i, 9) = "") And Not (Cells(i, 6) & Cells(i, 7) = "") Then
Cells(i, 16).Value = Cells(i, 6) & " - " & Cells(i, 7)

ElseIf (Cells(i, 7) = "") And (Cells(i, 8) = "") And Not (Cells(i, 6) & Cells(i, 9) = "") Then
Cells(i, 16).Value = Cells(i, 6) & " - " & Cells(i, 9)

'manque 3
ElseIf (Cells(i, 7) & Cells(i, 8) & Cells(i, 9) = "") And Not (Cells(i, 6) = "") Then
Cells(i, 16).Value = Cells(i, 6)


'sinon rien
Else
Cells(i, 16).Value = Cells(i, 6) & " - " & Cells(i, 7) & " - " & Cells(i, 8) & " - " & Cells(i, 9)
End If
Next i

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
 

Pièces jointes

  • Conception massifs test1.xlsm
    377.3 KB · Affichages: 40

Pierrot93

XLDnaute Barbatruc
Re : VBA - IF, ELSEIF, AND, And Not, ELSE

Bonjour,

pas ouvert ton fichier mais à priori ceci n'est pas correct :
Code:
ElseIf (Cells(i, 8) = "") And Not (Cells(i, 6) & Cells(i, 7) & Cells(i, 9) = "") Then

je coderais plutôt ainsi :
Code:
ElseIf (Cells(i, 8) = "") And Not Cells(i, 6)="" And Not  Cells(i, 7) ="" And Not  Cells(i, 9) = "" Then

A voir...

bonne journée
@+
 

JBARBE

XLDnaute Barbatruc
Re : VBA - IF, ELSEIF, AND, And Not, ELSE

Bonjour,

Pourquoi ne pas faire simple avec une fonction ( module 3 ) !

Code:
Function Concat(chaîne1, ParamArray chaîne2())
' fonction à nombre d'arguments indéfini
Dim Arg As Variant
' traitement du premier argument
Concat = chaîne1
' traitement des éventuels autres arguments
If UBound(chaîne2) <> -1 Then
 For Arg = LBound(chaîne2) To UBound(chaîne2)
  Concat = Concat & " " & chaîne2(Arg)
 Next Arg
End If
End Function

Bonne journée
 

Pièces jointes

  • Conception massifs.xlsm
    374.4 KB · Affichages: 54