XL 2010 Chaîne dans Chaînes sans doublon

cathodique

XLDnaute Barbatruc
Bonjour,

Je voudrais une fonction qui ajoute une catégorie précédée d'un slash (/) si elle ne figure pas dans la chaîne de la textbox1.

Je sélectionne l'idperson dans la combo1, dans textbox1 s'affiche la ou les catégorie(s).

ensuite, je sélectionne dans la combo2 une catégorie à ajouter, le résultat doit s'afficher dans textbox2.

si la catégorie se trouve déjà dans la textbox1, ne pas la rajouter
si la catégorie ne se trouve pas dans la textbox1, il faut la rajouter en la précédant d'un slash(/)

Je viens d'essayer d'exposer mon problème, en espérant être compris.

D'une manière générale, on ne doit pas avoir de chaine en doublon séparées par un slash (/)

Merci beaucoup
 

Pièces jointes

  • Cat sans doublon.xlsm
    21.5 KB · Affichages: 5
Solution
VB:
Private Function CatégorieMàJ() As String
If "/" & TextBox1.Text & "/" Like "*/" & Combo2.Text & "/*" _
  Or Combo2.Text = "" Then
   CatégorieMàJ = TextBox1.Text
ElseIf TextBox1.Text <> "" Then
   CatégorieMàJ = TextBox1.Text & "/" & Combo2.Text
Else
   CatégorieMàJ = Combo2.Text
   End If
End Function

cathodique

XLDnaute Barbatruc
Bonjour.
Je dirais :
VB:
Private Sub Combo2_Change()
If "/" & TextBox1.Text & "/" Like "*/" & Combo2.Text & "/*" Then Exit Sub
TextBox1.Text = IIf(TextBox1.Text <> "", TextBox1.Text & "/", "") & Combo2.Text
End Sub
Bonjour Dranreb;),

Je remercie pour ta proposition. Elle ne répond pas exactement à ma demande.
Ton code donne le résultat attendu mais agit dans la textbox1 au lieu de la textbox2.
Je voulais un fonction pour ne pas modifier ce qu'il y a dans la textbox1 car j'en ai besoin pour autre chose dans mon fichier de travail.

Merci beaucoup.
 

Dranreb

XLDnaute Barbatruc
Ou bien plutôt :
VB:
Private Sub Combo2_Change()
If "/" & TextBox1.Text & "/" Like "*/" & Combo2.Text & "/*" Then
   TextBox2.Text = TextBox1.Text
ElseIf TextBox1.Text <> "" Then
   TextBox2.Text = TextBox1.Text & "/" & Combo2.Text
Else
   TextBox2.Text = Combo2.Text
   End If
End Sub
 

job75

XLDnaute Barbatruc
Bonjour cathodique, Bernard,
VB:
Private Sub Combo1_Change()
TextBox1 = "": TextBox2 = "": Combo2 = ""
If Combo1.ListIndex >= 0 Then TextBox1 = Combo1.Column(1): TextBox2 = TextBox1
End Sub

Private Sub Combo2_Change()
If Combo1.ListIndex = -1 Or Combo2.ListIndex = -1 Then TextBox2 = "": Exit Sub
Dim x$
x = IIf(InStr("/" & TextBox1 & "/", "/" & Combo2 & "/") = 0, Combo2, "")
TextBox2 = TextBox2 & IIf(x = "", "", "/") & x
End Sub
A+
 

Pièces jointes

  • Cat sans doublon.xlsm
    25.2 KB · Affichages: 1

cathodique

XLDnaute Barbatruc
Ou bien plutôt :
VB:
Private Sub Combo2_Change()
If "/" & TextBox1.Text & "/" Like "*/" & Combo2.Text & "/*" Then
   TextBox2.Text = TextBox1.Text
ElseIf TextBox1.Text <> "" Then
   TextBox2.Text = TextBox1.Text & "/" & Combo2.Text
Else
   TextBox2.Text = Combo2.Text
   End If
End Sub
Je te remercie, donne le résultat escompté.
Je vais essayer de faire une fonction pour supprimer la textbox2.

Merci beaucoup.
 

cathodique

XLDnaute Barbatruc
Bonjour cathodique, Bernard,
VB:
Private Sub Combo1_Change()
TextBox1 = "": TextBox2 = "": Combo2 = ""
If Combo1.ListIndex >= 0 Then TextBox1 = Combo1.Column(1): TextBox2 = TextBox1
End Sub

Private Sub Combo2_Change()
If Combo1.ListIndex = -1 Or Combo2.ListIndex = -1 Then TextBox2 = "": Exit Sub
Dim x$
x = IIf(InStr("/" & TextBox1 & "/", "/" & Combo2 & "/") = 0, Combo2, "")
TextBox2 = TextBox2 & IIf(x = "", "", "/") & x
End Sub
A+
Bonjour Job75;),

Je te remercie. Avec ton code on peut ajouter autant de fois que l'on change la combo2.
Le but est d'avoir une fonction qui combine la chaine de la textbox1 et celle de la combo2 sans avoir de doublons séparés par le "/".
Le code de @Dranreb donne le résultat escompté. Je ne suis pas très à l'aise pour écrire des fonctions. Je dois tout de même essayer pour remplacer la textbox2 par une variable.

Merci beaucoup.
 

job75

XLDnaute Barbatruc
On peut se passer de TextBox2 :
VB:
Private Sub Combo1_Change()
TextBox1 = "": Combo2 = ""
If Combo1.ListIndex >= 0 Then TextBox1 = Combo1.Column(1)
End Sub

Private Sub Combo2_Change()
If Combo1.ListIndex = -1 Or Combo2.ListIndex = -1 Then TextBox1 = "": Exit Sub
Dim x$
x = IIf(InStr("/" & TextBox1 & "/", "/" & Combo2 & "/") = 0, Combo2, "")
TextBox1 = TextBox1 & IIf(x = "", "", "/") & x
End Sub
 

Pièces jointes

  • Cat sans doublon.xlsm
    25.8 KB · Affichages: 2

cathodique

XLDnaute Barbatruc
On peut se passer de TextBox2 :
VB:
Private Sub Combo1_Change()
TextBox1 = "": Combo2 = ""
If Combo1.ListIndex >= 0 Then TextBox1 = Combo1.Column(1)
End Sub

Private Sub Combo2_Change()
If Combo1.ListIndex = -1 Or Combo2.ListIndex = -1 Then TextBox1 = "": Exit Sub
Dim x$
x = IIf(InStr("/" & TextBox1 & "/", "/" & Combo2 & "/") = 0, Combo2, "")
TextBox1 = TextBox1 & IIf(x = "", "", "/") & x
End Sub
Je te remercie beaucoup.
Ce dernier donne le résultat escompté. Cependant, comme dit à @Dranreb au post#3, je voudrais pas modifier la textbox1 (dans mon fichier de travail, j'ai besoin de la valeur initial pour autre chose).
Comment faire de ton code une fonction?
Merci beaucoup.

@Dranreb : Désolé, je n'avais pas rafraichi du coup je n'ai pas vu tes post 4 et 5.
Mes excuses.
 

Dranreb

XLDnaute Barbatruc
VB:
Private Function CatégorieMàJ() As String
If "/" & TextBox1.Text & "/" Like "*/" & Combo2.Text & "/*" _
  Or Combo2.Text = "" Then
   CatégorieMàJ = TextBox1.Text
ElseIf TextBox1.Text <> "" Then
   CatégorieMàJ = TextBox1.Text & "/" & Combo2.Text
Else
   CatégorieMàJ = Combo2.Text
   End If
End Function
 

job75

XLDnaute Barbatruc
Pourquoi une fonction ?

Il suffit de restituer le résultat dans une MsgBox :
VB:
Private Sub Combo2_Change()
If Combo1.ListIndex = -1 Or Combo2.ListIndex = -1 Then TextBox1 = "": Exit Sub
Dim x$
x = IIf(InStr("/" & TextBox1 & "/", "/" & Combo2 & "/") = 0, Combo2, "")
MsgBox TextBox1 & IIf(x = "", "", "/") & x, , "Mise à jour Catégories"
End Sub
 

Pièces jointes

  • Cat sans doublon.xlsm
    26.2 KB · Affichages: 2

cathodique

XLDnaute Barbatruc
Pourquoi une fonction ?

Il suffit de restituer le résultat dans une MsgBox :
VB:
Private Sub Combo2_Change()
If Combo1.ListIndex = -1 Or Combo2.ListIndex = -1 Then TextBox1 = "": Exit Sub
Dim x$
x = IIf(InStr("/" & TextBox1 & "/", "/" & Combo2 & "/") = 0, Combo2, "")
MsgBox TextBox1 & IIf(x = "", "", "/") & x, , "Mise à jour Catégories"
End Sub
Bonjour @job75 ;),
Pourquoi une fonction ?
Pour stocker le groupe de catégories dans un ficher.

Ce dernier fonction parfaitement. Merci beaucoup.

Bon week-end
 

Discussions similaires

Membres actuellement en ligne

Statistiques des forums

Discussions
313 769
Messages
2 102 234
Membres
108 181
dernier inscrit
Chr1sD