Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1570 connectés 

  FORUM HardWare.fr
  Programmation
  VB/VBA/VBS

  Problème de code VBA

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Problème de code VBA

n°1518934
beneNC
Posté le 23-02-2007 à 15:00:56  profilanswer
 

:??: Bonjour,
 
je suis débutante en VBA (jai commencé en début de semaine). Je fais une boucle, à l'intérieur de laquelle je fais une trentaine de if imbriqués. Je me retrouve avec une erreur 438, qui a priori n'a pas de solution unique!
Est-ce-que quelqu'un pourrait m'aider svp?
 
Voici mon code (ouais je sais c'est un peu compliqué et long à lire...):
 
Sub RechercheCompo()
 
    Dim Cible1 As String
    Dim Cible2 As String
    Dim Cible3 As String
    Dim Cible4 As String
    Dim Cible5 As String
    Dim Cible6 As String
    Dim Cible7 As String
    Dim Cible8 As String
    Dim Cible9 As String
    Dim Cible10 As String
    Dim Cible11 As String
    Dim Cible12 As String
    Dim Cible13 As String
    Dim Cible14 As String
    Dim Cible15 As String
    Dim Cible16 As String
    Dim Cible17 As String
    Dim Cible18 As String
    Dim Cible19 As String
    Dim Cible20 As String
    Dim Cible21 As String
    Dim Cible22 As String
    Dim Cible23 As String
    Dim Cible24 As String
    Dim Cible25 As String
    Dim Cible26 As String
    Dim Cible27 As String
    Dim Cible28 As String
    Dim Cible29 As String
    Dim Cible30 As String
    Dim Cible31 As String
    Dim Cible32 As String
    Dim Cible33 As String
 
    Cible1 = "acier, fut"
    Cible2 = "acrylique"
    Cible3 = "alu"
    Cible4 = "bronze"
    Cible5 = "noir"
    Cible6 = " chr "
    Cible7 = " cuivre, cupro, cu/ni "
    Cible8 = " esther "
    Cible9 = " galva "
    Cible10 = " glycol "
    Cible11 = " diluant, gasoil "
    Cible12 = " inox, bague, clavette, écrou, collier, compact, cable, stainless, vis, entretoise, durite, vis, fourreau, manille, profil, outillage, tuyau, union "
    Cible13 = " roche "
    Cible14 = " laine de verre "
    Cible15 = " lait "
    Cible16 = " balsa "
    Cible17 = " adh "
    Cible18 = " nic "
    Cible19 = " intergard "
    Cible20 = " catalyseur "
    Cible21 = " plomb "
    Cible22 = "film"
    Cible23 = " crestomer, gel, garcette, resine, mastic"
    Cible24 = " gravicol, flacon "
    Cible25 = " polypro "
    Cible26 = " sika, polyur. "
    Cible27 = "pvc, taud"
    Cible28 = "fil roving, filet, mat, verre, pyrex, vitre "
    Cible29 = "plast, silicone, poignee, caout, flex, paulstra "
    Cible30 = " styrene "
    Cible31 = " caloretanche "
    Cible32 = " tissu roving "
    Cible33 = " vinylester "
     
    Dim i As Integer
 
i = 0
 
Do
 i = i + 1
 
If (InStr(Cible1, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
Sheets("nomenclature" ).cell(i, 6).Value = "acier" And Sheets("nomenclature" ).cell(i, 7).Value = "1H"
ElseIf (InStr(Cible2, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
Sheets("nomenclature" ).cell(i, 6).Value = "acrylique" And Sheets("nomenclature" ).cell(i, 7).Value = "1B"
 ElseIf (InStr(Cible3, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
 Sheets("nomenclature" ).cell(i, 6).Value = "aluminium" And Sheets("nomenclature" ).cell(i, 7).Value = "1H"
   ElseIf (InStr(Cible4, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
   Sheets("nomenclature" ).cell(i, 6).Value = "bronze" And Sheets("nomenclature" ).cell(i, 7).Value = "1H"
    ElseIf (InStr(Cible5, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
    Sheets("nomenclature" ).cell(i, 6).Value = "elastomère" And Sheets("nomenclature" ).cell(i, 7).Value = "3C"
     ElseIf (InStr(Cible6, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
     Sheets("nomenclature" ).cell(i, 6).Value = "chrome" And Sheets("nomenclature" ).cell(i, 7).Value = "1H"
      ElseIf (InStr(Cible7, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
      Sheets("nomenclature" ).cell(i, 6).Value = "cupro nickel" And Sheets("nomenclature" ).cell(i, 7).Value = "1H"
       ElseIf (InStr(Cible8, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
       Sheets("nomenclature" ).cell(i, 6).Value = "esther" And Sheets("nomenclature" ).cell(i, 7).Value = "1J"
        ElseIf (InStr(Cible9, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
        Sheets("nomenclature" ).cell(i, 6).Value = "galva" And Sheets("nomenclature" ).cell(i, 7).Value = "1H"
         ElseIf (InStr(Cible10, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
         Sheets("nomenclature" ).cell(i, 6).Value = "glycol" And Sheets("nomenclature" ).cell(i, 7).Value = "1F"
          ElseIf (InStr(Cible11, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
          Sheets("nomenclature" ).cell(i, 6).Value = "hydrocarbure" And Sheets("nomenclature" ).cell(i, 7).Value = "2C"
           ElseIf (InStr(Cible12, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
           Sheets("nomenclature" ).cell(i, 6).Value = "inox" And Sheets("nomenclature" ).cell(i, 7).Value = "1H"
            ElseIf (InStr(Cible13, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
            Sheets("nomenclature" ).cell(i, 6).Value = "laine de roche" And Sheets("nomenclature" ).cell(i, 7).Value = "1J"
             ElseIf (InStr(Cible14, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
             Sheets("nomenclature" ).cell(i, 6).Value = "laine de verre" And Sheets("nomenclature" ).cell(i, 7).Value = "1C"
              ElseIf (InStr(Cible15, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
              Sheets("nomenclature" ).cell(i, 6).Value = "laiton" And Sheets("nomenclature" ).cell(i, 7).Value = "1H"
               ElseIf (InStr(Cible16, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
               Sheets("nomenclature" ).cell(i, 6).Value = "matériau de construction" And Sheets("nomenclature" ).cell(i, 7).Value = "1H"
                ElseIf (InStr(Cible17, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                Sheets("nomenclature" ).cell(i, 6).Value = "mousse de polyéthylène" And Sheets("nomenclature" ).cell(i, 7).Value = "1C"
                 ElseIf (InStr(Cible18, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                 Sheets("nomenclature" ).cell(i, 6).Value = "nickel" And Sheets("nomenclature" ).cell(i, 7).Value = "1H"
                  ElseIf (InStr(Cible19, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                  Sheets("nomenclature" ).cell(i, 6).Value = "peinture" And Sheets("nomenclature" ).cell(i, 7).Value = "1B"
                   ElseIf (InStr(Cible20, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                   Sheets("nomenclature" ).cell(i, 6).Value = "peroxyde de méthyléthylcétone" And Sheets("nomenclature" ).cell(i, 7).Value = "3B"
                    ElseIf (InStr(Cible21, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                    Sheets("nomenclature" ).cell(i, 6).Value = "plomb" And Sheets("nomenclature" ).cell(i, 7).Value = "1H"
                     ElseIf (InStr(Cible22, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                     Sheets("nomenclature" ).cell(i, 6).Value = "polyamide" And Sheets("nomenclature" ).cell(i, 7).Value = "3C"
                      ElseIf (InStr(Cible23, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                      Sheets("nomenclature" ).cell(i, 6).Value = "polyester" And Sheets("nomenclature" ).cell(i, 7).Value = "1C"
                       ElseIf (InStr(Cible24, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                       Sheets("nomenclature" ).cell(i, 6).Value = "polyester et styrène" And Sheets("nomenclature" ).cell(i, 7).Value = "1C"
                        ElseIf (InStr(Cible25, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                        Sheets("nomenclature" ).cell(i, 6).Value = "polypropylène" And Sheets("nomenclature" ).cell(i, 7).Value = "1C"
                         ElseIf (InStr(Cible26, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                         Sheets("nomenclature" ).cell(i, 6).Value = "polyuréthane" And Sheets("nomenclature" ).cell(i, 7).Value = "1H"
                          ElseIf (InStr(Cible27, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                          Sheets("nomenclature" ).cell(i, 6).Value = "PVC" And Sheets("nomenclature" ).cell(i, 7).Value = "1C"
                           ElseIf (InStr(Cible28, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                           Sheets("nomenclature" ).cell(i, 6).Value = "résine de verre" And Sheets("nomenclature" ).cell(i, 7).Value = "1C"
                            ElseIf (InStr(Cible29, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                            Sheets("nomenclature" ).cell(i, 6).Value = "silicone" And Sheets("nomenclature" ).cell(i, 7).Value = "1B"
                             ElseIf (InStr(Cible30, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                             Sheets("nomenclature" ).cell(i, 6).Value = "styrène" And Sheets("nomenclature" ).cell(i, 7).Value = "1C"
                              ElseIf (InStr(Cible31, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                              Sheets("nomenclature" ).cell(i, 6).Value = "teflon" And Sheets("nomenclature" ).cell(i, 7).Value = "3C"
                               ElseIf (InStr(Cible32, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                               Sheets("nomenclature" ).cell(i, 6).Value = "tissu de verre" And Sheets("nomenclature" ).cell(i, 7).Value = "1C"
                                ElseIf (InStr(Cible33, Sheets("nomenclature" ).cell(i, 3)) <> 0) Then
                                Sheets("nomenclature" ).cell(i, 6).Value = "vynilesther" And Sheets("nomenclature" ).cell(i, 7).Value = "1C"
                                 
End If
 
Loop Until Sheets("nomenclature" ).cell(i, 3).Value = Null
 
 
 
End Sub
 


---------------
BénéNC
mood
Publicité
Posté le 23-02-2007 à 15:00:56  profilanswer
 

n°1518974
jpcheck
Pioupiou
Posté le 23-02-2007 à 15:48:30  profilanswer
 

Deja commence par mettre tous tes Dim en début de fonction... :)
Quel intérêt de mettre du texte dans tes cibles si tu ne t'en sers pas après ?

n°1518994
tegu
Posté le 23-02-2007 à 16:02:01  profilanswer
 

En dehors de l'illisibilité de ton code (et non pas de sa complexité), et de l'erreur dont tu parles quelque chose m'interpelle :

Sheets("nomenclature" ).cell(i, 6).Value = "acier" And Sheets("nomenclature" ).cell(i, 7).Value = "1H"

Cette ligne (prise en exemple mais les suivantes sont pareilles) affecte comme valeur le résultat d'une opération logique (And) entre une chaine ("acier" ) et une autre opération logique (résultat booléen vrai ou faux).
J'ai du mal à comprendre l'objectif.

 

À moins que les deux parties séparées par le mot "And" ne correspondent à deux lignes d'affectation différentes, auquel cas il faut écrire :

Sheets("nomenclature" ).cell(i, 6).Value = "acier"
Sheets("nomenclature" ).cell(i, 7).Value = "1H"


Merci de veiller à mieux indenter ton code et à utiliser les balises de formatage disponibles sur ce forum : [fixed][/fixed] ou [cpp][/cpp]

 

Pour l'erreur 438 en elle-même, il s'agit de l'objet « cell » que tu utilises : tu as oublié le S pour « Cells »

 

Je vais essayer de te proposer un code un peu plus avancé pour te montrer la différence
edit: finalement, tant que je ne comprendrais pas mieux cette histoire de "And" je peux difficilement te montrer un code plus efficace en terme d'algo.


Message édité par tegu le 23-02-2007 à 16:25:40
n°1519019
aprilthe5i​th
Posté le 23-02-2007 à 16:30:12  profilanswer
 

Effectivement.
Le mieux pour faire un code simle c'est de créer des tableurs :
 
dim MyTab()
dim a as integer
redim MyTab(0 to 32,0 to 1,0 to 4)
 
MyTab(0,0,0)="acier, fut"  
MyTabl(0,1,0)="acier"
MyTabl(0,0,1)="1H"
...
MyTab(32,0,0)=" vinylester "
MyTab(32,1,0)="vynilesther"  
MyTab(32,0,1)="1C"
 
do until isempty(Sheets("nomenclature" ).cell(i, 3))
  for a=0 to 32
    If (InStr(MyTab(a,0,0), Sheets("nomenclature" ).cell(i, 3)) then
      Sheets("nomenclature" ).cell(a+1, 6)= MyTab(a,1,0)
      Sheets("nomenclature" ).cell(a+1, 7)= MyTab(a,0,1)
    End if
  next a
loop
 
En fait tes "if" ne sont pas imbriqués, ils sont des conditions complémentaires, ils sont donc au même niveau hierarchique (un seul "End if" les clos.
 
Avec cette méthode, le remplissage des tes tables reste long, mais le code est très court et très simple.
   

n°1519020
aprilthe5i​th
Posté le 23-02-2007 à 16:32:21  profilanswer
 

Pardon, la déclaration des variables doit etre :
 
dim MyTab()
dim a as integer
redim MyTab(0 to 32,0 to 1,0 to 1)
 
Désolé

n°1519273
AprilThe5t​h
Posté le 24-02-2007 à 02:56:53  profilanswer
 

Pfff... mon code est plein d'erreurs... je l'ai écris d'un trait...
 
Correction :
 
dim MyTab()
dim a as integer
redim MyTab(0 to 32,0 to 1,0 to 1)
 
MyTab(0,0,0)="acier, fut"  
MyTabl(0,1,0)="acier"
MyTabl(0,0,1)="1H"
...
MyTab(32,0,0)=" vinylester "
MyTab(32,1,0)="vynilesther"  
MyTab(32,0,1)="1C"
 
do until isempty(Sheets("nomenclature" ).cells(i, 3))
  for a=0 to 32
    If InStr(MyTab(a,0,0), Sheets("nomenclature" ).cells(i, 3)) then
      Sheets("nomenclature" ).cells(a+1, 6)= MyTab(a,1,0)
      Sheets("nomenclature" ).cells(a+1, 7)= MyTab(a,0,1)
    End if
  next a
loop  
 
 
re-désolé

n°1519645
beneNC
Posté le 25-02-2007 à 15:44:51  profilanswer
 

Merci à tous pour vos réponses
j'essaie ce que vous m'avez expliqué.

n°1519671
beneNC
Posté le 25-02-2007 à 17:15:41  profilanswer
 

Merci à AprilThe5th: ça marche trop bien ta boucle!
ça me simplifie le boulot c'est énorme!


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  VB/VBA/VBS

  Problème de code VBA

 

Sujets relatifs
Problème encodage des caractères et xml_http_requestVBA - Gif animé dans un Userfom [Résolu]
[RESOLU]PHP et MySQL problème avec des boucles!![VB.NET] Problème au déploiement d'une appli avec Winsock
[VB] besoin d'aide pour un problèmeARGH -->probleme ds script navigation
[Réglé] Problème avec ma fonction d'encodage de chaine[MySql]Probleme requete interval date début- date fin pour réservation
Problème d'accent dans ma base de donnéeProbleme code VBA de exel 97 -> 2000
Plus de sujets relatifs à : Problème de code VBA


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR