Bonjour à tous,
j'ai le code suivant qui recherche une valeur "serial" dans un fichier DB.xlsx en appuyant sur un bouton et m'affiche le nbr d'occurrences dans un label sur userform ainsi que les différentes données que j'ai besoin de voir dans textbox/labels. J'aimerais créer un autre bouton dans mon userform pour faire un back search de mes recherches. Pour ce faire, je pense qu'il faut declarer des variables public?
Merci de votre aide
j'ai le code suivant qui recherche une valeur "serial" dans un fichier DB.xlsx en appuyant sur un bouton et m'affiche le nbr d'occurrences dans un label sur userform ainsi que les différentes données que j'ai besoin de voir dans textbox/labels. J'aimerais créer un autre bouton dans mon userform pour faire un back search de mes recherches. Pour ce faire, je pense qu'il faut declarer des variables public?
Merci de votre aide
Code:
Private Sub CommandButton2_Click()
Dim match As Integer
Dim val0 As String
serial = UserFormMain.TextBox1.value
Set xls_fichier = GetObject("c:\DB.xlsm")
xls_fichier.Windows(1).Visible = True
xls_fichier.Sheets("DB2").Select
With xls_fichier.Sheets("DB2").range("b2:b2000")
Set c = .Find(serial, LookIn:=xlValues, lookat:=xlWhole)
If Not c Is Nothing Then
FirstAddress = c.Address
Do
UserFormMain.TextBox6.value = c.Offset(0, 3).value
UserFormMain.TextBox2.value = c.Offset(0, 10).value
UserFormMain.TextBox4.value = c.Offset(0, 5).value
UserFormMain.TextBox3.value = c.Offset(0, 9).value
UserFormMain.Label14.Caption = c.Offset(0, 6).value
If c.Offset(0, 7).value = "Production" Then
UserFormMain.Optionproduction.value = True
Else
UserFormMain.OptionRMA.value = True
End If
If c.Offset(0, 11).value = "Oui" Then
UserFormMain.OptionButton1.value = True
Else
UserFormMain.OptionButton2.value = True
End If
match = match + 1
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> FirstAddress
Else
val0 = "no match"
UserFormMain.Label17.Caption = ""
UserFormMain.TextBox6.value = ""
UserFormMain.TextBox2.value = ""
UserFormMain.TextBox4.value = ""
UserFormMain.TextBox3.value = ""
UserFormMain.Label14.Caption = ""
UserFormMain.Optionproduction.value = False
UserFormMain.OptionRMA.value = False
UserFormMain.OptionButton1.value = False
UserFormMain.OptionButton2.value = False
End If
End With
xls_fichier.Sheets("DB1").Select
With xls_fichier.Sheets("DB1").range("b2:b1000")
Set c = .Find(serial, LookIn:=xlValues, lookat:=xlWhole)
If Not c Is Nothing Then
FirstAddress = c.Address
Do
UserFormMain.TextBox7.value = c.Offset(0, 3).value
UserFormMain.TextBox8.value = c.Offset(0, 5).value
UserFormMain.Label17.Caption = c.Offset(0, 6).value
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> FirstAddress
Else
UserFormMain.TextBox7.value = ""
UserFormMain.TextBox8.value = ""
' UserFormMain.Label17.Caption = ""
' UserFormMain.TextBox6.value = ""
' UserFormMain.TextBox2.value = ""
' UserFormMain.TextBox4.value = ""
' UserFormMain.TextBox3.value = ""
' UserFormMain.Label14.Caption = ""
' UserFormMain.Optionproduction.value = False
' UserFormMain.OptionRMA.value = False
' UserFormMain.OptionButton1.value = False
' UserFormMain.OptionButton2.value = False
End If
End With
xls_fichier.Close SaveChanges:=False
Set xls_fichier = Nothing
If val0 = "no match" Then
MsgBox "La recherche n'abouti à rien!", vbCritical
val0 = 1
End If
Label21.Caption = match
End Sub