Précédent   Forum des professionnels en informatique > Logiciels > Microsoft Office > Excel > Macros et VBA Excel
Macros et VBA Excel Vos questions relatives aux macros Excel, à l'utilisation de VBA et à l'automatisation de vos classeurs Excel.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 23/08/2011, 17h00   #1
Invité régulier
 
Inscription : février 2009
Messages : 70
Détails du profil
Informations forums :
Inscription : février 2009
Messages : 70
Points : 9
Points : 9
Par défaut boucle de condition

Bonjour,

mon code identifie dans des chaines de caractères certains caractères et selon les caractères identifiés renvoie une réponse différente.

Lorsque le programme s'exécute les chaines de caractères examinées passent dans tous les cas de ma boucle et comme mes chaines sont très longues, ils peut y avoir plusieurs cas possibles et brouiller la réponse attendue.

Je voudrais que lorsque les caractères sont identifiés, le programme passe directement à la ligne suivante de mon tableur excel contenant la chaine de caractères suivante à examiner.

Autrement dit : cherche "INB " si oui retourne le résultat et passe à la chaine de caractère suivante ; si non cherche "eva/" etc etc...

Est ce qu'il ne me manque donc pas quelque chose dans ma boucle de condition ?

Merci d'avance pour votre aide. J'espère être claire. Pardon pour les noms de variables bizarres.

Voici mon code
Code :
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
59
60
61
62
63
64
65
66
67
68
69
70
 
 
Private Sub CommandButton1_Click()
 
 Dim i As Integer
 
    Dim Chaine As String
 
    Dim Caract As String * 4
 
    Dim Voyel As String
 
    Dim objRange, cel As Range
    Dim res As Integer
 
Range("C1").EntireColumn.SpecialCells(xlCellTypeConstants).Select
 
For Each objRange In Selection
 
  For i = 1 To Len(objRange)
  Caract = Mid(objRange, i, 4)
 
        Select Case LCase(Caract)
 
        Case "inb "
 
        Voyel = Voyel & Mid(objRange, i, 7)
        objRange.Offset(0, -2).Value = Right(Voyel, 3)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 3)), Sheets("REF").Range("$A$1:$B$95"), 2, False)
 
        Case "eva/"
 
        Voyel = Voyel & Mid(objRange, i, 6)
        objRange.Offset(0, -2).Value = Right(Voyel, 2)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$I$2:$J$9"), 2, False)
 
        Case "udd/"
 
        Voyel = Voyel & Mid(objRange, i, 6)
        objRange.Offset(0, -2).Value = Right(Voyel, 2)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$G$2:$H$10"), 2, False)
 
        Case "cea/"
 
        Voyel = Voyel & Mid(objRange, i, 6)
        objRange.Offset(0, -2).Value = Right(Voyel, 2)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$K$2:$L$7"), 2, False)
 
        Case "dra/"
 
        Voyel = Voyel & Mid(objRange, i, 6)
        objRange.Offset(0, -2).Value = Right(Voyel, 2)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$M$2:$N$6"), 2, False)
 
        Case "nts/"
 
        Voyel = Voyel & Mid(objRange, i, 6)
        objRange.Offset(0, -2).Value = Right(Voyel, 2)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$O$2:$P$15"), 2, False)
 
         Case Else
 
         End Select
 
       Next i
 
 
  Next objRange
 
End Sub
marie6631 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/08/2011, 17h33   #2
Expert Confirmé Sénior
 
Avatar de jfontaine
 
Homme Jérôme FONTAINE
Contrôleur de Gestion
Inscription : juin 2006
Messages : 3 894
Détails du profil
Informations personnelles :
Nom : Homme Jérôme FONTAINE
Âge : 38
Localisation : France, Sarthe (Pays de la Loire)

Informations professionnelles :
Activité : Contrôleur de Gestion

Informations forums :
Inscription : juin 2006
Messages : 3 894
Points : 7 177
Points : 7 177
Bonjour,

A première vue, je sortirais de la boucle for en donnant à i la valeur de sortie de boucle

Soit dans ton code

Code :
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
Private Sub CommandButton1_Click()
 
 Dim i As Integer
 
    Dim Chaine As String
 
    Dim Caract As String * 4
 
    Dim Voyel As String
 
    Dim objRange, cel As Range
    Dim res As Integer
 
Range("C1").EntireColumn.SpecialCells(xlCellTypeConstants).Select
 
For Each objRange In Selection
 
  For i = 1 To Len(objRange)
  Caract = Mid(objRange, i, 4)
 
        Select Case LCase(Caract)
 
        Case "inb "
 
        Voyel = Voyel & Mid(objRange, i, 7)
        objRange.Offset(0, -2).Value = Right(Voyel, 3)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 3)), Sheets("REF").Range("$A$1:$B$95"), 2, False)
        i = Len(objRange)
 
        Case "eva/"
 
        Voyel = Voyel & Mid(objRange, i, 6)
        objRange.Offset(0, -2).Value = Right(Voyel, 2)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$I$2:$J$9"), 2, False)
        i = Len(objRange)
 
        Case "udd/"
 
        Voyel = Voyel & Mid(objRange, i, 6)
        objRange.Offset(0, -2).Value = Right(Voyel, 2)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$G$2:$H$10"), 2, False)
        i = Len(objRange)
 
        Case "cea/"
 
        Voyel = Voyel & Mid(objRange, i, 6)
        objRange.Offset(0, -2).Value = Right(Voyel, 2)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$K$2:$L$7"), 2, False)
        i = Len(objRange)
 
        Case "dra/"
 
        Voyel = Voyel & Mid(objRange, i, 6)
        objRange.Offset(0, -2).Value = Right(Voyel, 2)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$M$2:$N$6"), 2, False)
        i = Len(objRange)
 
        Case "nts/"
 
        Voyel = Voyel & Mid(objRange, i, 6)
        objRange.Offset(0, -2).Value = Right(Voyel, 2)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$O$2:$P$15"), 2, False)
        i = Len(objRange)
 
         Case Else
 
         End Select
 
       Next i
 
 
  Next objRange
 
End Sub
__________________
Jérôme

Citation:
"Ils ne savaient pas que c'était impossible, alors ils l'ont fait" - Marc Twain
Si la réponse répond à votre besoin, votre vote nous encouragera.
Dans le cas ou la réponse mérite, à vos yeux, un , nous faire partager la raison de ce vote, pourrait nous permettre de nous améliorer.
jfontaine est actuellement connecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/08/2011, 17h36   #3
Membre confirmé
 
Inscription : juillet 2007
Messages : 209
Détails du profil
Informations forums :
Inscription : juillet 2007
Messages : 209
Points : 219
Points : 219
Bonjour,

snas regarder de trop près dans ton code , je vois quelques erreurs :

Code :
1
2
3
4
5
6
7
8
 
 
For i = 1 To Len(objRange)
  Caract = Mid(objRange, i, 4)
 
'  problème car lorsque i > Len(objRange) - 4 =>    Mid(objRange, i, 4)  n'est pas défini , tu peux remplacer par   For i = 1 To Len(objRange) - 3
 
' mais problème dans la suite du code , tu utilises Mid(objRange, i, 7) ?
Je ne comprends pas trop le but du code ; A mon avis , tu devrais utiliser une syntaxe du type

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
if instr (   lcase ( objRange) , "eva/" ) > 0 then 
 
Voyel = Voyel & Mid(objRange, instr (   lcase ( objRange) , "eva/" ) , 7 + instr (   lcase ( objRange) , "eva/" ) )
        objRange.Offset(0, -2).Value = Right(Voyel, 3)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 3)), Sheets("REF").Range("$A$1:$B$95"), 2, False)
 
 
 
 
else
 
 
.......
Tu devrais donner plus d'infosou un fichier avec le type des chaines à analyser.
CodeFacile est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/08/2011, 20h11   #4
Membre Expert
 
Homme Hervé Silve
Inscription : août 2010
Messages : 773
Détails du profil
Informations personnelles :
Nom : Homme Hervé Silve
Localisation : France

Informations forums :
Inscription : août 2010
Messages : 773
Points : 2 093
Points : 2 093
Bonsoir,

Je ne suis pas sûr d'avoir tout compris mais teste ce qui suit :
Code :
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 
Private Sub CommandButton1_Click()
 
    Dim Pos As Integer
    Dim Voyel As String
    Dim objRange As Range
    Dim cel As Range
 
    'défini la plage de cellules
    Set objRange = Range("C1").EntireColumn.SpecialCells(xlCellTypeConstants)
 
    'parcour la plage
    For Each cel In objRange
 
        'effectue la recherche
        Pos = InStr(cel, "inb ")
 
        'si trouvé
        If Pos <> 0 Then
 
            Voyel = Voyel & Mid(cel, Pos, 7)
            cel.Offset(0, -2).Value = Right(Voyel, 3)
            cel.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 3)), Sheets("REF").Range("$A$1:$B$95"), 2, False)
            GoTo FinCel 'va à la fin de la boucle
 
        End If
 
        Pos = InStr(cel, "eva/")
 
        If Pos <> 0 Then
 
            Voyel = Voyel & Mid(objRange, Pos, 6)
            cel.Offset(0, -2).Value = Right(Voyel, 2)
            cel.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$I$2:$J$9"), 2, False)
            GoTo FinCel 'va à la fin de la boucle
 
        End If
 
        Pos = InStr(cel, "udd/")
 
        If Pos <> 0 Then
 
            Voyel = Voyel & Mid(cel, Pos, 6)
            cel.Offset(0, -2).Value = Right(Voyel, 2)
            cel.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$G$2:$H$10"), 2, False)
            GoTo FinCel 'va à la fin de la boucle
 
        End If
 
        Pos = InStr(cel, "cea/")
 
        If Pos <> 0 Then
 
            Voyel = Voyel & Mid(cel, Pos, 6)
            cel.Offset(0, -2).Value = Right(Voyel, 2)
            cel.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$K$2:$L$7"), 2, False)
            GoTo FinCel 'va à la fin de la boucle
 
        End If
 
        Pos = InStr(cel, "dra/")
 
        If Pos <> 0 Then
 
            Voyel = Voyel & Mid(cel, Pos, 6)
            cel.Offset(0, -2).Value = Right(Voyel, 2)
            cel.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$M$2:$N$6"), 2, False)
            GoTo FinCel 'va à la fin de la boucle
 
        End If
 
        Pos = InStr(cel, "nts/")
 
        If Pos <> 0 Then
 
            Voyel = Voyel & Mid(cel, Pos, 6)
            cel.Offset(0, -2).Value = Right(Voyel, 2)
            cel.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$O$2:$P$15"), 2, False)
            'GoTo FinCel ici pas nécessaire !
        End If
 
FinCel:
 
      Next cel
 
End Sub
Hervé.
Theze est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 24/08/2011, 14h13   #5
Invité régulier
 
Inscription : février 2009
Messages : 70
Détails du profil
Informations forums :
Inscription : février 2009
Messages : 70
Points : 9
Points : 9
Merci pour vos réponses.
Je voulais savoir s'il y a un équivalent du break en C pour VBA pour sortir de ma boucle. Je vais tester vos propositions.
marie6631 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/08/2011, 14h26   #6
Membre actif
 
Inscription : novembre 2008
Messages : 188
Détails du profil
Informations forums :
Inscription : novembre 2008
Messages : 188
Points : 194
Points : 194
Citation:
Envoyé par marie6631 Voir le message
Merci pour vos réponses.
Je voulais savoir s'il y a un équivalent du break en C pour VBA pour sortir de ma boucle.
Oui, pour une boucle For...Next, c'est Exit For.
Sclarckone est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/08/2011, 14h44   #7
Invité régulier
 
Inscription : février 2009
Messages : 70
Détails du profil
Informations forums :
Inscription : février 2009
Messages : 70
Points : 9
Points : 9
La réponse de JFontaine fonctionne à un détail prés.

Ma chaine de caractère est de type :

type 1 :
/01 INB/01 XXXXX/01 XXX INB/01 XXXX/01 XXXX/02 XXXX/16 INB YYY - NOM/XXX,/04 XXX/01 XXX/01 XXX/XXX/XXXXXX/ INB YYY
Cela fonctionne : mon résultat est YYY

type 2 :
/01 INB/09 CEA/02 ZZZ/03 - INB YYY - NOM/03 XXX/XXXXXXXXX- INB YYY
dans ce cas le résultat est ZZZ associé à la recherche de "cea/" et non YYY associée à la recherche "inb ".

Je ne comprends pas pourquoi dans ce dernier cas cela ne fonctionne pas. Je voudrais le résultat YYY comme dans le type 1 puisque "inb " est présent. Je voudrais ZZZ que s'il n'y a que "cea/".

Merci pour votre aide. J'espère être claire

Voici le code pour rappel :

Code :
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
Private Sub CommandButton1_Click()
 
 Dim i As Integer
 
    Dim Chaine As String
 
    Dim Caract As String * 4
 
    Dim Voyel As String
 
    Dim objRange, cel As Range
    Dim res As Integer
 
Range("C1").EntireColumn.SpecialCells(xlCellTypeConstants).Select
 
For Each objRange In Selection
 
  For i = 1 To Len(objRange)
  Caract = Mid(objRange, i, 4)
 
        Select Case LCase(Caract)
 
        Case "inb "
 
        Voyel = Voyel & Mid(objRange, i, 7)
        objRange.Offset(0, -2).Value = Right(Voyel, 3)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 3)), Sheets("REF").Range("$A$1:$B$95"), 2, False)
        i = Len(objRange)
 
        Case "eva/"
 
        Voyel = Voyel & Mid(objRange, i, 6)
        objRange.Offset(0, -2).Value = Right(Voyel, 2)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$I$2:$J$9"), 2, False)
        i = Len(objRange)
 
        Case "udd/"
 
        Voyel = Voyel & Mid(objRange, i, 6)
        objRange.Offset(0, -2).Value = Right(Voyel, 2)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$G$2:$H$10"), 2, False)
        i = Len(objRange)
 
        Case "cea/"
 
        Voyel = Voyel & Mid(objRange, i, 6)
        objRange.Offset(0, -2).Value = Right(Voyel, 2)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$K$2:$L$7"), 2, False)
        i = Len(objRange)
 
        Case "dra/"
 
        Voyel = Voyel & Mid(objRange, i, 6)
        objRange.Offset(0, -2).Value = Right(Voyel, 2)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$M$2:$N$6"), 2, False)
        i = Len(objRange)
 
        Case "nts/"
 
        Voyel = Voyel & Mid(objRange, i, 6)
        objRange.Offset(0, -2).Value = Right(Voyel, 2)
        objRange.Offset(0, -1).Value = Application.WorksheetFunction.VLookup(Val(Right(Voyel, 2)), Sheets("REF").Range("$O$2:$P$15"), 2, False)
        i = Len(objRange)
 
         Case Else
 
         End Select
 
       Next i
 
 
  Next objRange
 
End Sub
Pardon mais je veux sortir de ma boucle de condition et non de la boucle for.

merci d'avance
marie6631 est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 25/08/2011, 11h02   #8
Membre actif
 
Inscription : novembre 2008
Messages : 188
Détails du profil
Informations forums :
Inscription : novembre 2008
Messages : 188
Points : 194
Points : 194
Citation:
je veux sortir de ma boucle de condition et non de la boucle for.
Je ne connais pas d'instruction similaire au "Break" en C permettant de sortir de ton Select...End Select.

Ceci dit à moins que plusieurs conditions puissent être vérifiées ça ne te coûtera pas grand chose de le laisser finir de tester les Case.

Au pire tu peux toujours t'en sortir avec Label un GoTo...
Sclarckone est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 25/08/2011, 12h01   #9
Responsable
Office & Excel

 
Avatar de Pierre Fauconnier
 
Homme Pierre Fauconnier
Formateur et développeur informatique indépendant
Inscription : novembre 2003
Messages : 8 198
Détails du profil
Informations personnelles :
Nom : Homme Pierre Fauconnier
Âge : 45
Localisation : Belgique

Informations professionnelles :
Activité : Formateur et développeur informatique indépendant
Secteur : Enseignement

Informations forums :
Inscription : novembre 2003
Messages : 8 198
Points : 14 398
Points : 14 398
Envoyer un message via Skype™ à Pierre Fauconnier
Salut

Citation:
Envoyé par marie6631 Voir le message
Pardon mais je veux sortir de ma boucle de condition et non de la boucle for.

merci d'avance


Lorsqu'un CASE est validé, les CASE suivants ne sont pas évalués et le code saute directement sur End Select
__________________
"Plus les hommes seront éclairés, plus ils seront libres" (Voltaire)
---------------

Ma nouvelle vidéo: comparer des listes via une MFC - Mes articles sur DVP
Vous souhaitez rédiger pour DVP? Contactez-moi
Amoureux de la langue française? Venez corriger nos ressources
VBA pour Excel? Pensez D'ABORD en EXCEL avant de penser en VBA...
N'oubliez pas de VOTER (en bas à droite d'un message)

---------------
Pierre Fauconnier est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 26/08/2011, 08h11   #10
Invité régulier
 
Inscription : février 2009
Messages : 70
Détails du profil
Informations forums :
Inscription : février 2009
Messages : 70
Points : 9
Points : 9
Merci pour votre réponse. Mon pb est désormais résolu.
Bonne journée
marie6631 est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 16h44.


 
 
 
 
Partenaires

Hébergement Web