Recherche et écriture

  • Initiateur de la discussion Initiateur de la discussion JessCH
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

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
 
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+
 
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

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
 
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

- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Réponses
3
Affichages
897
L
Réponses
2
Affichages
670
Retour