Doublons

Provence Vintage

XLDnaute Occasionnel
Comment puis-je trouver une parade!

Un Usf m'enregistre via macro données dans feuille.
1ère colonne de ma feuille "Nom"
Le hic: si on a deux fois le même Nom, et oui, ça arrive, comment forcer pour une saisie du genre, "existe déjà" ou "mettre un autre Nom" ou d'office "Nom"2
Avec le code d'origine qui enregistre dans ma feuille:

Private Sub CommandButton1_Click()
Application.ScreenUpdating = False


Sheets("Base de Données acheteurs").Activate
Dim NomDeFeuilEnCours$, lidep1 As Long, cellule As Range
NomDeFeuilEnCours = "Base de Données acheteurs"
Dim Ctrl As Control
Dim Valeur As String
Dim Vr As Byte, Fx As Byte
Dim Coul&
CoulRouge = 3: CoulNoir = 1
PalRouge& = &HC0&: PalNoir& = &H80000008




For Each Ctrl In Me.Controls
If TypeOf Ctrl Is MSForms.CheckBox Then
If Ctrl.Value = True Then
Valeur = Valeur & Ctrl.Name & " = True " & Chr(10)
Vr = Vr + 1
Else
Valeur = Valeur & Ctrl.Name & " =False " & Chr(10)
Fx = Fx + 1
If CréerFicheAcquéreur.TextBox9.Value = "" Then
MsgBox " Le Nom de l'acquéreur est obligatoire . "
Exit Sub
End If
End If
End If
Next


li = Range("A6000").End(xlUp).Row
li = li + IIf(li < 4, 2, 1) ' à cause des lignes fusionnées !
Cells(li, 1) = TextBox9.Value
Cells(li, 2) = TextBox10.Value
Cells(li, 2) = TextBox10.Value
Cells(li, 3) = TextBox5.Value
Cells(li, 4) = TextBox24.Value
Cells(li, 5) = TextBox25.Value
Cells(li, 6) = TextBox22.Value
Cells(li, 7) = IIf(OptionButton15, "Tous secteurs", "")
Cells(li, 16) = IIf(CheckBox1, "MDV", "") & IIf(CheckBox2, "Appart", "") & IIf(CheckBox3, "Villa", "") & IIf(CheckBox4, "Mas", "") & IIf(CheckBox5, "Terrain", "")
Cells(li, 17) = TextBox2.Value
Cells(li, 18) = IIf(CheckBox11, "Oui", "")
Cells(li, 19) = TextBox3.Value
Cells(li, 20) = TextBox4.Value
Cells(li, 21) = IIf(OptionButton1, "Oui", "")
Cells(li, 22) = IIf(OptionButton2, "Oui", "")
Cells(li, 23) = TextBox7.Value
Cells(li, 24) = TextBox8.Value
Cells(li, 25) = IIf(CheckBox6, "Oui", "")
Cells(li, 26) = IIf(CheckBox7, "Oui", "")
Cells(li, 27) = IIf(CheckBox8, "Oui", "")
Cells(li, 28) = IIf(CheckBox9, "Oui", "")
Cells(li, 29) = IIf(OptionButton11, "Cuisine US", "")
Cells(li, 29) = IIf(OptionButton10, "Cuisine séparée", "")
Cells(li, 30) = IIf(CheckBox10, "Oui", "")
Cells(li, 31) = TextBox20.Value
Cells(li, 32) = TextBox21.Value
Cells(li, 33) = IIf(CheckBox12, "Oui", "")
Cells(li, 34) = IIf(CheckBox13, "Oui", "")
Cells(li, 35) = TextBox23.Value
Cells(li, 36) = IIf(CheckBox14, "Oui", "")
Cells(li, 37) = TextBox19.Value
If ComboBox1.ForeColor = PalNoir& Then c = CoulNoir Else c = CoulRouge
Cells(li, 8).Value = ComboBox1.Value
Cells(li, 8).Font.ColorIndex = c
If ComboBox2.ForeColor = PalNoir& Then c = CoulNoir Else c = CoulRouge
Cells(li, 9).Value = ComboBox2.Value
Cells(li, 9).Font.ColorIndex = c
If ComboBox3.ForeColor = PalNoir& Then c = CoulNoir Else c = CoulRouge
Cells(li, 10).Value = ComboBox3.Value
Cells(li, 10).Font.ColorIndex = c
If ComboBox4.ForeColor = PalNoir& Then c = CoulNoir Else c = CoulRouge
Cells(li, 11).Value = ComboBox4.Value
Cells(li, 11).Font.ColorIndex = c
If ComboBox5.ForeColor = PalNoir& Then c = CoulNoir Else c = CoulRouge
Cells(li, 12).Value = ComboBox5.Value
Cells(li, 12).Font.ColorIndex = c
If ComboBox6.ForeColor = PalNoir& Then c = CoulNoir Else c = CoulRouge
Cells(li, 13).Value = ComboBox6.Value
Cells(li, 13).Font.ColorIndex = c
If ComboBox7.ForeColor = PalNoir& Then c = CoulNoir Else c = CoulRouge
Cells(li, 14).Value = ComboBox7.Value
Cells(li, 14).Font.ColorIndex = c
If ComboBox8.ForeColor = PalNoir& Then c = CoulNoir Else c = CoulRouge
Cells(li, 15).Value = ComboBox8.Value
Cells(li, 15).Font.ColorIndex = c

Call trierzonedetriacheteurs
Call calculnombrefichesacheteurs
Unload Me


Application.ScreenUpdating = True
SaisirPije.Hide
Accueil.Show
CréerFicheAcquéreur.Hide
Accueil.Show
End Sub


La plus importante étant: Cells(li, 1) = TextBox9.Value
puisque c celle qui me check le nom ds ma base de données.....

Faut'il passer par (exemple):

Etre averti lors de la saisie des doublons dans la plage A1:A5000

Procédure à placer au niveau de la feuille en utilisant l'evenement "Change"

Private Sub Worksheet_Change(byVal Target As Excel.Range)

If Target.Column = 1 Then

If Application.worksheetFunction.countIf(Range("A1:A5 000"), Target.Value) > 1 Then msgBox "ce nom existe déja"

End If

End Sub
 

JNP

XLDnaute Barbatruc
Re : Doublons

Bonjour Provence Vintage :),
Un zeste de politesse (Lien suppriméserait le bienvenu :mad:...
Bref, personnellement, j'utiliserais l'évènement Exit du TextBox9 pour vérifier si le nom existe déjà dans la BDD, et afficher les informations du client déjà enregistré pour vérifier si c'est un doublon ou un homonyme. Ensuite, je n'aimerais pas changer le nom d'un client, parce qu'un autre porte le même. Je modifierais la BDD pour rentrer une clef primaire (un N° incrémentable par exemple, ou une codification sur plusieurs éléments qui ont peu de chance d'être identiques, comme 3 lettres du prénom, 3 lettres du nom et 3 lettres de la ville), et quand je chercherais un client par le nom, je proposerais tous ceux enregistrés dans la BDD.
Bon WE :cool:
 

Provence Vintage

XLDnaute Occasionnel
Re : Doublons

JNP, Le Fil,

Pour l'evenement Exit, je ne vois pas trop comment m'y prendre!
Tu afficherais les doublons comment! via listview? en utisant à la fonction recherche
regarder si en offset +1 il n'y a pas le même nom...

Pour la clef, alors là ça cale complétement, j'ai regarder aussi les sujets sur concatener pour rechercher éventuellement sur Nom + Prénom + n° Tel
ça rend la recherche peux-être un peu lourde!

Merci à ttes et tous pour vos commentaires
a+
 

JNP

XLDnaute Barbatruc
Re : Doublons

Re :),
Quelquechose comme cela
Code:
Private Sub TextBox9_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Range("A1:A500").Find(TextBox9.Text) Is Nothing Then Exit Sub
MsgBox "Il existe déjà un client de ce nom"
' ...
End Sub
Les ... peuvent être la montée d'un USF avec une ListView par exemple.
Si tu concatènes Nom + Prénom + Tél., ça va pas rendre la recherche un peu lourde, au contraire, plus la chaine est définie, plus la recherche est rapide... mais prends plutot les derniers chiffres du tél., ça sera plus causant que 06...
Code:
ClefPrimaire = Left(Nom, 3) & Left(Prénom, 3) & Right(Téléphone, 6)
Le but de la clef primaire, c'est que chaque ligne soit considérée comme une ligne unique de la BDD.
Bon courage :cool:
 

Provence Vintage

XLDnaute Occasionnel
Re : Doublons

JNP, Le Fil,

Private Sub TextBox9_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Range("A1:A500").Find(TextBox9.Text) Is Nothing Then Exit Sub
MsgBox "Il existe déjà un client de ce nom"
' ... c là ou tu viens glissé USFLISTVIEW.Show par exemple

End Sub

et après tu monte ta liste c ça!?
 

Provence Vintage

XLDnaute Occasionnel
Re : Doublons

JNP, Le Fil,

Oui mais là.....
ClefPrimaire
Tu le colle où, (je suis débutant en VBA, fais ce que je peux)
Dim ClefPrimaire et ...
Oups; là je crois que j'ai besoin d'aide séreieux,
Voici mon code de recherche:
Private Sub CommandButton1_Click()

Dim cel As Range
Dim ClefPrimaire
ClefPrimaire = Left(Nom, 3) & Left(Prénom, 3) & Right(Téléphone, 6)
If TextBox23 = "" Then
MsgBox " Le Nom de l'acheteur est obligatoire . "

Else
Label44.Caption = "Recherche de " & TextBox23

Worksheets("Base de Données acheteurs").Activate
Set cel = Range("A1")
Set cel = Columns(1).Find(What:=TextBox23, After:=cel, _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If Not cel Is Nothing Then
L = cel.Row


Label45.Caption = "éxiste déjà " & L
TextBox24 = Cells(L, "B")
TextBox25 = Cells(L, "C")
TextBox26 = Cells(L, "D")
TextBox27 = Cells(L, "E")
TextBox28 = Cells(L, "F")
TextBox29 = Cells(L, "G")
TextBox30 = Cells(L, "H")
TextBox31 = Cells(L, "I")
TextBox32 = Cells(L, "J")
TextBox33 = Cells(L, "K")
TextBox34 = Cells(L, "L")
TextBox35 = Cells(L, "M")
TextBox36 = Cells(L, "N")
TextBox37 = Cells(L, "O")
TextBox38 = Cells(L, "P")
TextBox39 = Cells(L, "Q")
TextBox40 = Cells(L, "R")
TextBox41 = Cells(L, "S")
TextBox42 = Cells(L, "T")
TextBox43 = Cells(L, "U")
TextBox44 = Cells(L, "V")
TextBox45 = Cells(L, "W")
TextBox46 = Cells(L, "X")
TextBox47 = Cells(L, "Y")
TextBox48 = Cells(L, "Z")
TextBox49 = Cells(L, "AA")
TextBox50 = Cells(L, "AB")
TextBox51 = Cells(L, "AC")
TextBox52 = Cells(L, "AD")
TextBox53 = Cells(L, "AE")
TextBox54 = Cells(L, "AF")
TextBox55 = Cells(L, "AG")
TextBox56 = Cells(L, "AH")
TextBox57 = Cells(L, "AI")
TextBox58 = Cells(L, "AJ")
TextBox59 = Cells(L, "AK")
Sheets("ImprimFicheAcheteur").Range("B8") = TextBox23
Sheets("ImprimFicheAcheteur").Range("F8") = Cells(L, "C")
Sheets("ImprimFicheAcheteur").Range("F13") = Cells(L, "G")
Sheets("ImprimFicheAcheteur").Range("C11") = Cells(L, "P")
Sheets("ImprimFicheAcheteur").Range("B1") = Cells(L, "Q")
Sheets("ImprimFicheAcheteur").Range("C24") = Cells(L, "W")
Sheets("ImprimFicheAcheteur").Range("C26") = Cells(L, "U")
Sheets("ImprimFicheAcheteur").Range("C25") = Cells(L, "S")
Sheets("ImprimFicheAcheteur").Range("C27") = Cells(L, "T")
Sheets("ImprimFicheAcheteur").Range("C28") = Cells(L, "V")
Sheets("ImprimFicheAcheteur").Range("C29") = Cells(L, "Y")
Sheets("ImprimFicheAcheteur").Range("C30") = Cells(L, "AJ")
Sheets("ImprimFicheAcheteur").Range("B13") = Cells(L, "H")
Sheets("ImprimFicheAcheteur").Range("B14") = Cells(L, "I")
Sheets("ImprimFicheAcheteur").Range("B15") = Cells(L, "J")
Sheets("ImprimFicheAcheteur").Range("B16") = Cells(L, "K")
Sheets("ImprimFicheAcheteur").Range("B17") = Cells(L, "L")
Sheets("ImprimFicheAcheteur").Range("B18") = Cells(L, "M")
Sheets("ImprimFicheAcheteur").Range("B19") = Cells(L, "N")
Sheets("ImprimFicheAcheteur").Range("B20") = Cells(L, "O") & Cells(L, "O").Font.ColorIndex
Sheets("ImprimFicheAcheteur").Range("F2") = Cells(L, "AI")
Sheets("ImprimFicheAcheteur").Range("F1") = Cells(L, "F")
Sheets("ImprimFicheAcheteur").Range("C3") = Cells(L, "R")
Sheets("ImprimFicheAcheteur").Range("C4") = Cells(L, "AG")
Sheets("ImprimFicheAcheteur").Range("C5") = Cells(L, "AE")
Sheets("ImprimFicheAcheteur").Range("C6") = Cells(L, "AF")
Sheets("ImprimFicheAcheteur").Range("A32") = Cells(L, "AK")
If Cells(L, "Z") = "Oui" Then
CheckBox1 = True
Else: Chekbox1 = False

End If

With Me.TextBox30
.BackColor = Cells(L, "H").Interior.Color
.ForeColor = Cells(L, "H").Font.Color
End With



Else
MsgBox "Pas trouvé"
TextBox23 = ""
TextBox23.SetFocus
End If
'End With
End If


TextBox23 = ""

End Sub

Merci à ttes et tous
 

JNP

XLDnaute Barbatruc
Re : Doublons

</SPAN>Re :),
Débutant en VBA ? Je ne m'en serais pas douté :p... T'inquiètes, on est tous passé par là ;).
Clef Primaire : Clef unique d'enregistrement permettant un identifiant unique pour un enregistrement (comprendre une ligne dans le cas d'Excel). Cette clef primaire, par habitude, on la mets plutôt en 1ere colonne (A), mais ce n'est pas une obligation, surtout si ça t'oblige à tout changer :D. Dès qu'on parle de BDD, il vaut mieux commencer par ça (mais même moi, j'oublie :eek:, mais je suis pas une référence en BDD, loin de là :eek:).
Ensuite, si tu fait remonter toutes les données de l'acheteur qui a le même nom dans le même USF, bonjour l'angoisse... Je pensais plutôt à un petit
Code:
Dim Cellule As Range
Set Cellule = Range("A1:A500").Find(TextBox9.Text)
MsgBox Cellule & " " & Cellule.Offset(0, 1) & " " _
    & Cellule.Offset(0, 2) & "existe déjà."
qui peut être amélioré avec un FindNext pour remonter tous les noms homonymes existant.
Enfin,
Code:
TextBox24 = Cells(L, "B")
TextBox25 = Cells(L, "C")
TextBox26 = Cells(L, "D")
TextBox27 = Cells(L, "E")
TextBox28 = Cells(L, "F")
TextBox29 = Cells(L, "G")
TextBox30 = Cells(L, "H")
TextBox31 = Cells(L, "I")
TextBox32 = Cells(L, "J")
TextBox33 = Cells(L, "K")
TextBox34 = Cells(L, "L")
TextBox35 = Cells(L, "M")
TextBox36 = Cells(L, "N")
TextBox37 = Cells(L, "O")
TextBox38 = Cells(L, "P")
TextBox39 = Cells(L, "Q")
TextBox40 = Cells(L, "R")
TextBox41 = Cells(L, "S")
TextBox42 = Cells(L, "T")
TextBox43 = Cells(L, "U")
TextBox44 = Cells(L, "V")
TextBox45 = Cells(L, "W")
TextBox46 = Cells(L, "X")
TextBox47 = Cells(L, "Y")
TextBox48 = Cells(L, "Z")
TextBox49 = Cells(L, "AA")
TextBox50 = Cells(L, "AB")
TextBox51 = Cells(L, "AC")
TextBox52 = Cells(L, "AD")
TextBox53 = Cells(L, "AE")
TextBox54 = Cells(L, "AF")
TextBox55 = Cells(L, "AG")
TextBox56 = Cells(L, "AH")
TextBox57 = Cells(L, "AI")
TextBox58 = Cells(L, "AJ")
TextBox59 = Cells(L, "AK")
peut être simplifié en
Code:
Dim I As Integer
For I = 24 To 59
Controls("TextBox" & I) = Cells(L, I - 22)
Next I
et
Code:
Sheets("ImprimFicheAcheteur").Range("B8") = TextBox23
Sheets("ImprimFicheAcheteur").Range("F8") = Cells(L, "C")
Sheets("ImprimFicheAcheteur").Range("F13") = Cells(L, "G")
Sheets("ImprimFicheAcheteur").Range("C11") = Cells(L, "P")
Sheets("ImprimFicheAcheteur").Range("B1") = Cells(L, "Q")
Sheets("ImprimFicheAcheteur").Range("C24") = Cells(L, "W")
Sheets("ImprimFicheAcheteur").Range("C26") = Cells(L, "U")
Sheets("ImprimFicheAcheteur").Range("C25") = Cells(L, "S")
Sheets("ImprimFicheAcheteur").Range("C27") = Cells(L, "T")
Sheets("ImprimFicheAcheteur").Range("C28") = Cells(L, "V")
Sheets("ImprimFicheAcheteur").Range("C29") = Cells(L, "Y")
Sheets("ImprimFicheAcheteur").Range("C30") = Cells(L, "AJ")
Sheets("ImprimFicheAcheteur").Range("B13") = Cells(L, "H")
Sheets("ImprimFicheAcheteur").Range("B14") = Cells(L, "I")
Sheets("ImprimFicheAcheteur").Range("B15") = Cells(L, "J")
Sheets("ImprimFicheAcheteur").Range("B16") = Cells(L, "K")
Sheets("ImprimFicheAcheteur").Range("B17") = Cells(L, "L")
Sheets("ImprimFicheAcheteur").Range("B18") = Cells(L, "M")
Sheets("ImprimFicheAcheteur").Range("B19") = Cells(L, "N")
Sheets("ImprimFicheAcheteur").Range("B20") = Cells(L, "O") & Cells(L, "O").Font.ColorIndex
Sheets("ImprimFicheAcheteur").Range("F2") = Cells(L, "AI")
Sheets("ImprimFicheAcheteur").Range("F1") = Cells(L, "F")
Sheets("ImprimFicheAcheteur").Range("C3") = Cells(L, "R")
Sheets("ImprimFicheAcheteur").Range("C4") = Cells(L, "AG")
Sheets("ImprimFicheAcheteur").Range("C5") = Cells(L, "AE")
Sheets("ImprimFicheAcheteur").Range("C6") = Cells(L, "AF")
Sheets("ImprimFicheAcheteur").Range("A32") = Cells(L, "AK")
peut s'écrire
Code:
With Sheets("ImprimFicheAcheteur")
.Range("B8") = TextBox23
.Range("F8") = Cells(L, "C")
.Range("F13") = Cells(L, "G")
.Range("C11") = Cells(L, "P")
.Range("B1") = Cells(L, "Q")
.Range("C24") = Cells(L, "W")
.Range("C26") = Cells(L, "U")
.Range("C25") = Cells(L, "S")
.Range("C27") = Cells(L, "T")
.Range("C28") = Cells(L, "V")
.Range("C29") = Cells(L, "Y")
.Range("C30") = Cells(L, "AJ")
.Range("B13") = Cells(L, "H")
.Range("B14") = Cells(L, "I")
.Range("B15") = Cells(L, "J")
.Range("B16") = Cells(L, "K")
.Range("B17") = Cells(L, "L")
.Range("B18") = Cells(L, "M")
.Range("B19") = Cells(L, "N")
.Range("B20") = Cells(L, "O") & Cells(L, "O").Font.ColorIndex
.Range("F2") = Cells(L, "AI")
.Range("F1") = Cells(L, "F")
.Range("C3") = Cells(L, "R")
.Range("C4") = Cells(L, "AG")
.Range("C5") = Cells(L, "AE")
.Range("C6") = Cells(L, "AF")
.Range("A32") = Cells(L, "AK")
End With
Ce qui amméliorerait la lisibilité de ton code.
Bon courage :cool:
 

Provence Vintage

XLDnaute Occasionnel
Re : Doublons

JNP, klin89, Le Fil,

je cale pour utiliser Find Next et avoir les résultats des homonymes existant dans une lIstView

"Code:

Dim Cellule As Range
Set Cellule = Range("A1:A500").Find(TextBox9.Text)
MsgBox Cellule & " " & Cellule.Offset(0, 1) & " " _
& Cellule.Offset(0, 2) & "existe déjà."

qui peut être amélioré avec un FindNext pour remonter tous les noms homonymes existant."

Comment????
Merci à toutes et tous
 

Provence Vintage

XLDnaute Occasionnel
Re : Doublons

JNP, Le Fil,

Voici mon adaptation à ton code:
Private Sub Nom_afterupdate()
Dim Cellule As Range, PremièreAdresse As String
With Worksheets("bdd acheteur").Range("B2:B" & Range("B65536").End(xlUp).Row)
Set Cellule = .Find(Nom.Text)
If Not Cellule Is Nothing Then
PremièreAdresse = Cellule.Address
Do
If Cells(Cellule.Row, 2).Value = Nom.Text Then GoTo Solution
Set Cellule = .FindNext(Cellule)
Loop While Not Cellule Is Nothing And Cellule.Address <> PremièreAdresse
MsgBox "Pas de correspondance"
Exit Sub
End If
End With
Solution:
RechetCréaAcquéreur.Label45.Caption = Cellule & " " & "existe déjà" & " Ligne " & "" & Cellule.Row


Le soucis, c que le test que je fais me dis en effet existe déjà, mais pas toutes les lignes, dans mon test en ligne 3 et 4!
bien entendu à l'enregistrement je tri la base,
Mais imaginons que l'on ai 3 homonymes (c pour comprendre), comment pourrait t'on dire et oui, ligne 3 + ligne 4 + ligne 5....

Ensuite, comment afficher les résultats dans une List?

Pour Finir, si on part du principe que l'on enregistre dans la base avec une clef primaire,
comment fait on la recherche:
avec la clef primaire ou le nom, si c avec clef primaire, c pas pratique!

J'ai toujours du mal a définir dans mon code + haut, à l'enregistrement dans la base, la clef primaire.

Pour finir, et là grand merci, en effet, tes commentaires simplifient grandement les codes....

Bon après-midi à tous et toutes
 

Provence Vintage

XLDnaute Occasionnel
Re : Doublons

JNP, Le Fil,

j'ai des bugs:
Private Sub Nom_afterupdate()
Dim cellule As Range, PremièreAdresse As String

Sheets("bdd acheteur").Activate
With Worksheets("bdd acheteur").Range("B2:B" & Range("B65536").End(xlUp).Row)
Set cellule = .Find(Nom.Text)
If Not cellule Is Nothing Then
PremièreAdresse = cellule.Address
Do
If Cells(cellule.Row, 2).Value = Nom.Text Then GoTo Solution
Set cellule = .FindNext(cellule)
Loop While Not cellule Is Nothing And cellule.Address <> PremièreAdresse
MsgBox "N'éxiste Pas"
Exit Sub
End If
End With

Solution:
RechetCréaAcquéreur.Label45.Caption = "Ligne N°" & cellule.Row


End Sub

Lorsque je renseigne un nom qui est dans la liste, tt va bien, quand c n'est pas le cas, tout bug!
Merci pour vos commentaires
 

Statistiques des forums

Discussions
312 229
Messages
2 086 424
Membres
103 206
dernier inscrit
diambote