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 22/08/2011, 15h27   #1
Membre du Club
 
Homme
Inscription : janvier 2004
Messages : 239
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 30
Localisation : France

Informations forums :
Inscription : janvier 2004
Messages : 239
Points : 63
Points : 63
Envoyer un message via MSN à Chicard
Par défaut supression de lignes vides

Bonjour,

J'ai un formulaire dont certains champs peuvent rester vides (la condition n'étant pas gênantes) et donc le retour mes tous dans une seule cellule (ce qui est le but recherché).
Le souci c'est voilà mon code (en considérant que tous les champs soient remplis):
Code :
1
2
3
4
 text = TextBox_nom.text & Chr(10) & TextBox_adresse.text & Chr(10) & TextBox_code_postal.text & _
        Chr(32) & TextBox_ville.text & Chr(32) & TextBox_cedex.text & Chr(10) & _
        TextBox_prenom_referent.text & Chr(32) & TextBox_nom_referent.text & _
        Chr(10) & TextBox_telephone.text & Chr(10) & TextBox_fax.text
Ce code fonctionne. Seulement si par exemple les textbox contenant le nom et le prénom du référant sont vides cela me fait une ligne vide dans ma cellule.

Je pourrais faire le bourrin avec une série de if mais je voudrais savoir si il y a un moyen plus subtil ou pas.
Chicard est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/08/2011, 15h52   #2
Membre régulier
 
Homme Florian
Étudiant
Inscription : mai 2011
Messages : 44
Détails du profil
Informations personnelles :
Nom : Homme Florian
Âge : 21
Localisation : France, Ille et Vilaine (Bretagne)

Informations professionnelles :
Activité : Étudiant

Informations forums :
Inscription : mai 2011
Messages : 44
Points : 73
Points : 73
Salut à toi,
tu peux utiliser les IIf (Immediate If) qui fonctionnent comme ça :
IIf([CONDITION], [SI VRAI], [SI FAUX])
Du coup j'écrirais ton code comme ça :
Code :
1
2
3
4
5
6
7
8
9
text = IIf(TextBox_nom.text<>vbNullString, TextBox_nom.text & Chr(10), vbNullString) & _
       IIf(TextBox_adresse.text<>vbNullString, TextBox_adresse.text & Chr(10), vbNullString) & _
       IIf(TextBox_code_postal.text<>vbNullString, TextBox_code_postal.text & Chr(32), vbNullString) & _
       IIf(TextBox_ville.text<>vbNullString, TextBox_ville.text & Chr(32), vbNullString) & _
       IIf(TextBox_cedex.text<>vbNullString, TextBox_nom.text & Chr(10), vbNullString) & _
       IIf(TextBox_prenom_referent.text<>vbNullString, TextBox_prenom_referent.text & Chr(32), vbNullString) & _
       IIf(TextBox_nom_referent.text<>vbNullString, TextBox_nom_referent.text & Chr(10), vbNullString) & _
       IIf(TextBox_telephone.text<>vbNullString, TextBox_telephone.text & Chr(10), vbNullString) & _ 
       TextBox_fax.text
C'est plein de If, mais c'est plus propre que des If / Then
Orhleil est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/08/2011, 16h00   #3
Membre du Club
 
Homme
Inscription : janvier 2004
Messages : 239
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 30
Localisation : France

Informations forums :
Inscription : janvier 2004
Messages : 239
Points : 63
Points : 63
Envoyer un message via MSN à Chicard
Effectivement, c'est vrai que j'avais plus dans l'idée une série de if/then. Je pense que je pourrais presque utiliser ça pour autre chose.

Mais avant de mettre un "résolu" je vais attendre voir s'il y a mieux ou pas.
Chicard est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 22/08/2011, 17h25   #4
Membre confirmé
 
Inscription : juillet 2007
Messages : 209
Détails du profil
Informations forums :
Inscription : juillet 2007
Messages : 209
Points : 219
Points : 219
Bonjour,

Pour une solution élégante et simple syntaxiquement , tu peux t'appuyer sur ce type :


Code :
1
2
3
4
5
6
coordonne_client  =""
for each control_ in array (  textbox_nom , textbox_prenom , textbox_ville )
 
if control_ = ville  then separ = chr (32) else separ = chr (10)
coordonne_client   = coordonne_client   & IIf(control_.Value = "", "", control_.value & separ ) 
next
CodeFacile est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/08/2011, 11h25   #5
Membre du Club
 
Homme
Inscription : janvier 2004
Messages : 239
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 30
Localisation : France

Informations forums :
Inscription : janvier 2004
Messages : 239
Points : 63
Points : 63
Envoyer un message via MSN à Chicard
je vois pas bien à quoi sert cette ligne:
Code :
coordonne_client   = coordonne_client   & IIf(control_.Value = "", "", control_.value & separ)

Par contre sur les IIF j'ai rajouté une ligne
Code :
1
2
3
4
5
6
7
8
9
10
IIf(TextBox_nom.text<>vbNullString, TextBox_nom.text & Chr(10), vbNullString) & _
       IIf(TextBox_complementaire.text<>vbNullString, TextBox_complementaire.text & Chr(10), vbNullString) & _
       IIf(TextBox_adresse.text<>vbNullString, TextBox_adresse.text & Chr(10), vbNullString) & _
       IIf(TextBox_code_postal.text<>vbNullString, TextBox_code_postal.text & Chr(32), vbNullString) & _
       IIf(TextBox_ville.text<>vbNullString, TextBox_ville.text & Chr(32), vbNullString) & _
       IIf(TextBox_cedex.text<>vbNullString, TextBox_nom.text & Chr(10), vbNullString) & _
       IIf(TextBox_prenom_referent.text<>vbNullString, TextBox_prenom_referent.text & Chr(32), vbNullString) & _
       IIf(TextBox_nom_referent.text<>vbNullString, TextBox_nom_referent.text & Chr(10), vbNullString) & _
       IIf(TextBox_telephone.text<>vbNullString, TextBox_telephone.text & Chr(10), vbNullString) & _
       IIf(TextBox_fax.text<>vbNullString, TextBox_fax.text, vbNullString)
et je me retrouve avec l'erreur :
Citation:
erreur de compilation :
attendu: =
et surligner en rouge le premier & de fin de ligne (la fin du premier IIF), je vois pas pourquoi.
Chicard est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/08/2011, 16h11   #6
Membre du Club
 
Homme
Inscription : janvier 2004
Messages : 239
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 30
Localisation : France

Informations forums :
Inscription : janvier 2004
Messages : 239
Points : 63
Points : 63
Envoyer un message via MSN à Chicard
J'ai résolu le souci de l'erreur
Code :
1
2
3
4
5
6
7
8
9
10
text = IIf(TextBox_nom.text <> vbNullString, TextBox_nom.text & Chr(10), vbNullString) & _
       IIf(TextBox_complementaire.text <> vbNullString, TextBox_complementaire.text & Chr(10), vbNullString) & _
       IIf(TextBox_adresse.text <> vbNullString, TextBox_adresse.text & Chr(10), vbNullString) & _
       IIf(TextBox_code_postal.text <> vbNullString, TextBox_code_postal.text & Chr(32), vbNullString) & _
       IIf(TextBox_ville.text <> vbNullString, TextBox_ville.text & Chr(32), vbNullString) & _
       IIf(TextBox_cedex.text <> vbNullString, TextBox_nom.text & Chr(10), vbNullString) & _
       IIf(TextBox_prenom_referent.text <> vbNullString, TextBox_prenom_referent.text & Chr(32), vbNullString) & _
       IIf(TextBox_nom_referent.text <> vbNullString, TextBox_nom_referent.text & Chr(10), vbNullString) & _
       IIf(TextBox_telephone.text <> vbNullString, TextBox_telephone.text & Chr(10), vbNullString) & _
       IIf(TextBox_fax.text <> vbNullString, TextBox_fax.text, vbNullString)
j'aurais bien aimé essayer l'autre solution mais j'ai de léger souci à la mise en place.

Merci.
Chicard est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/08/2011, 16h30   #7
Membre confirmé
 
Inscription : juillet 2007
Messages : 209
Détails du profil
Informations forums :
Inscription : juillet 2007
Messages : 209
Points : 219
Points : 219
Bonjour,

je reprends mon explication car je vois que je n'ai pas été compris.

Avec la solution de Orhleil , il y a autant de IIF imbriqués qu'il y a de champs au formulaire . C'est dur à maintenir si on ajoute un champs à une place donnée du formulaire.

Avec ma solution , on décrit un tableau /array où sont énumérés tous les champs du formulaire dans l'ordre où l'on veut les voir apparaitre dans la structure text ( coordonne_client == text ) .
Pour adapter ma solution à ton code , je commence l'énumération ( à toi de finir ..)
Code :
1
2
3
4
5
6
7
8
9
10
11
12
 
text  =""
 
for each control_ in array (  TextBox_nom , TextBox_complementaire ,  TextBox_adresse ,TextBox_code_postal , 'et tous le reste des textbox  dans l'ordre souhaité pour text_' )
 
 
if control_ = TextBox_ville then separ = chr (32) else separ = chr (10)
 
 text = text & IIf(control_.Value = "", "", control_.value & separ)
 
 
next control_
Et il n'y a pas plus de code à faire
CodeFacile est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 23/08/2011, 16h42   #8
Membre du Club
 
Homme
Inscription : janvier 2004
Messages : 239
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 30
Localisation : France

Informations forums :
Inscription : janvier 2004
Messages : 239
Points : 63
Points : 63
Envoyer un message via MSN à Chicard
Effectivement je l'avait pas compris comme ça. J'essayerais peut-être demain et si je n'y arrive pas (sait-on jamais quand on me connait) ça restera comme c'est actuellement mais merci pour l'explication.
Chicard 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 09h43.


 
 
 
 
Partenaires

Hébergement Web