Bonjour!
J'ai un petit souci de programmation avec virtual basic.
Je dois créer une macro capable de rechercher une référence dans un fichier excel.
Je débute en VB, mais je "sais" programmer en C/C++
Mon souci est le suivant:
J'ai une ListBox qui affiche des mots clés (suite à une recherche). J'aimerais pouvoir selectionner un élément de cette liste et récuperer l'emplacement de celle-ci pour pouvoir afficher la ligne complète (dans excel) dans une autre ListBox.
J'ai bien tenté d'utiliser ListIndex mais sans résultat convenable. Je ne suis pas sûr de bien comprendre son fonctionnement
Voici le code qui execute la recherche (après un click sur un bouton):
Et voici le pseudo code que j'ai tenté de taper pour au moins réafficher le mot clé sélectionné dans la ListBox1 vers la ListBox2
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 Private Sub CommandButton1_Click() Dim ref As String Dim export As Worksheet Dim MotCle As String Dim erreur As String Set export = Sheets("A") Dim i As Integer If OptionButton1.Value = False And OptionButton2.Value = False Then erreur = MsgBox("Veuillez choisir une méthode de recherche", vbOKOnly + vbCritical, "Erreur") End If If OptionButton1.Value = True Then ref = InputBox("Saisir la référence recherchée") With Worksheets(1).Columns(1) Set c = .Find(What:=ref, LookAt:=xlWhole) If ref = ("") Then ref = MsgBox("Veuillez entrer au moins un caractère", vbCritical + vbOKOnly, "Attention") ElseIf c Is Nothing Then MsgBox ("Référence inexistante, affichage des références les plus ressemblantes") Set c = .Find(What:=ref, LookAt:=xlPart) If Not c Is Nothing Then firstAddress = c.Address Do Set c = .FindNext(c) ListBox1.AddItem c CommandButton3.Enabled = True Loop While Not c Is Nothing And c.Address <> firstAddress End If Else CommandButton3.Enabled = True ListBox1.AddItem c End If End With End If If OptionButton2.Value = True Then MotCle = InputBox("Saisir un mot clé") With Worksheets(1).Columns(2) Set d = .Find(What:=MotCle, LookAt:=xlPart) If Not d Is Nothing Then firstAddress = d.Address Do Set d = .FindNext(d) If MotCle = ("") Then MotCle = MsgBox("Veuillez entrer au moins un caractère", vbCritical + vbOKOnly, "Attention") ElseIf d Is Nothing Then MsgBox ("Mot(s) clé(s) non trouvé(s)") Else ListBox1.AddItem d CommandButton3.Enabled = True End If Loop While Not d Is Nothing And d.Address <> firstAddress End If End With End If End Sub
Evidemment ça ne marche pas du tout ^^
Ca écris juste 0
Merci d'avance pour votre aide.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 Private Sub CommandButton2_Click() UserForm4.Height = 500 ListBox2.Enabled = True ListBox2.Visible = True Dim Compteur As Integer Dim Detail As String Compteur = ListBox1.ListCount If Compteur > 0 Then Detail = ListBox1.ListIndex End If ListBox2.AddItem Detail End Sub
Partager