Recherche et écriture

JessCH

XLDnaute Nouveau
Bonjour à vous,

Voilà, j'ai une question et j'espère enfin trouver ma réponse dans ce forum.

J'ai un UserForm avec trois ComboBox et un TextBox. Selon mes données de la ComboBox 1; 2 et 3, je voudrais écrire le texte de ma TextBox dans la cellule cible.
Par Exemple:

Si ComboBox1 = "4008" ; ComboBox2 = "3" ; ComboBox3 = x Alors Cellule "C8" = TextBox1

Mais attention, je ne veux pas indiquer sur le programme comme ça:
If ComboBox1 = "4008" And ComboBox2 = "3" And ComboBox3 ="x" Then Range "C5" = TextBox1
Je veux qu'il y ai une recherche dans le tableau pour trouver la cellule et y écrire le texte de la TextBox

J'espère être assez clair
 

job75

XLDnaute Barbatruc
Re,

Le code de UserForm2 :
Code:
Private Sub TextBox1_Change()
If ComboBox1.ListIndex = -1 Then ComboBox1 = "": ComboBox1.SetFocus: Exit Sub
If ComboBox2.ListIndex = -1 Then ComboBox2 = "": ComboBox2.SetFocus: Exit Sub
If ComboBox3.ListIndex = -1 Then ComboBox3 = "": ComboBox3.SetFocus: Exit Sub
Dim t$, tablo, i&
t = ComboBox2 & ComboBox3
With Feuil2 'CodeName de la feuille
    tablo = .[A1].CurrentRegion.Resize(, 2) 'tableau VBA, plus rapide
    For i = 2 To UBound(tablo)
        If tablo(i, 1) & tablo(i, 2) = t Then
            .Cells(i, Application.Match(Val(ComboBox1), .Rows(1), 0)) = TextBox1
            Exit Sub
        End If
    Next
End With
End Sub
A+
 

job75

XLDnaute Barbatruc
Re,

Avec un Dictionary la recherche est beaucoup plus rapide sur un grand tableau :
Code:
Dim d As Object 'mémorise la variable

Private Sub TextBox1_Change()
If ComboBox1.ListIndex = -1 Then ComboBox1 = "": ComboBox1.SetFocus: Exit Sub
If ComboBox2.ListIndex = -1 Then ComboBox2 = "": ComboBox2.SetFocus: Exit Sub
If ComboBox3.ListIndex = -1 Then ComboBox3 = "": ComboBox3.SetFocus: Exit Sub
With Feuil2 'CodeName de la feuille
    .Cells(d(ComboBox2 & ComboBox3), Application.Match(Val(ComboBox1), .Rows(1), 0)) = TextBox1
End With
End Sub

Private Sub UserForm_Initialize()
Dim tablo, i&
tablo = Feuil2.[A1].CurrentRegion.Resize(, 2) 'tableau VBA, plus rapide
Set d = CreateObject("Scripting.Dictionary")
For i = 2 To UBound(tablo)
    d(tablo(i, 1) & tablo(i, 2)) = i 'repère la ligne
Next
End Sub
Fichier joint.

A+
 

Pièces jointes

  • TEST(1).xlsm
    29 KB · Affichages: 29

JessCH

XLDnaute Nouveau
Re,
ça marche super, mais j'ai besoin dans certains cas aussi que de 2 données. C'est a dire pour la ComboBox1 et la ComboBox2 pour positionner le text de ma TextBox dans mon tableau.

Comme tu t'en doute je débute, je me suis pas mal demmerder jusqu'a présent, mais je bloque pour cette situation.

Par avance MERCI
 

job75

XLDnaute Barbatruc
Re,

Avec uniquement ComboBox1 et ComboBox2 :
Code:
Dim d As Object 'mémorise la variable

Private Sub TextBox1_Change()
If ComboBox1.ListIndex = -1 Then ComboBox1 = "": ComboBox1.SetFocus: Exit Sub
If ComboBox2.ListIndex = -1 Then ComboBox2 = "": ComboBox2.SetFocus: Exit Sub
With Feuil2 'CodeName de la feuille
    .Cells(d(ComboBox2.Text), Application.Match(Val(ComboBox1), .Rows(1), 0)) = TextBox1
End With
End Sub

Private Sub UserForm_Initialize()
Dim tablo, i&
tablo = Feuil2.[A1].CurrentRegion.Resize(, 2) 'tableau VBA, plus rapide, au moins 2 éléments
Set d = CreateObject("Scripting.Dictionary")
For i = 2 To UBound(tablo)
    d(CStr(tablo(i, 1))) = i 'repère la ligne
Next
End Sub
Fichier (2).

A+
 

Pièces jointes

  • TEST(2).xlsm
    28.1 KB · Affichages: 23

Discussions similaires

Statistiques des forums

Discussions
312 190
Messages
2 086 040
Membres
103 105
dernier inscrit
fofana