Option Compare Text
Dim nomTableau, TblBD(), nbCol
Private Sub UserForm_Initialize()
nomTableau = "Tableau1"
nbCol = Range(nomTableau).Columns.Count
TblBD = Range(nomTableau).Resize(, nbCol + 1).Value ' Array: + rapide
For i = 1 To UBound(TblBD): TblBD(i, nbCol + 1) = i: Next i ' No enregistrement
LabelsTextBox
TextBoxRecherche_Change
End Sub
Private Sub TextBoxRecherche_Change()
colRecherche = 1
colRecherche2 = 2
clé = Me.TextBoxRecherche & "*": n = 0
Dim Tbl()
For i = 1 To UBound(TblBD)
If TblBD(i, colRecherche) Like clé Or TblBD(i, colRecherche2) Like clé Then
n = n + 1: ReDim Preserve Tbl(1 To 3, 1 To n)
Tbl(1, n) = TblBD(i, colRecherche): Tbl(2, n) = TblBD(i, colRecherche2)
Tbl(3, n) = TblBD(i, nbCol + 1)
End If
Next i
If n > 0 Then Me.Listbox1.Column = Tbl Else Me.Listbox1.Clear
End Sub
Private Sub Listbox1_Click()
ligneEnreg = Me.Listbox1.Column(2)
Me.Enreg = ligneEnreg
For k = 1 To nbCol
Me("textbox" & k) = TblBD(ligneEnreg, k)
Next k
End Sub
Sub LabelsTextBox()
For c = 1 To nbCol
Me("textbox" & c).Width = Range(nomTableau).Columns(c).Width * 1.3
tmp = Range(nomTableau).Offset(-1).Item(1, c)
Me("label" & c).Caption = tmp
lg = Len(tmp): If Len(tmp) > 20 Then lg = 20
Me("label" & c).Width = lg * 8
Next
End Sub
Sub raz()
For k = 1 To nbCol
Me("textBox" & k) = ""
Next k
Me.TextBox1.SetFocus
End Sub
Private Sub B_sup_Click()
If Me.Enreg <> "" Then
If MsgBox("Etes vous sûr de supprimer " & Me.TextBox1 & "?", vbYesNo) = vbYes Then
Range(nomTableau).Rows(Me.Enreg).Delete
Me.Enreg = ""
UserForm_Initialize
raz
Me.Enreg = Range(nomTableau).Rows.Count + 1
End If
End If
End Sub
Private Sub B_ajout_Click()
raz
Me.Enreg = Range(nomTableau).Rows.Count + 1
End Sub
Private Sub B_validation_Click()
Enreg = Me.Enreg
For c = 1 To nbCol
If Not Range(nomTableau).Item(Enreg, c).HasFormula Then
tmp = Me("textbox" & c)
If IsNumeric(Replace(tmp, ".", ",")) And InStr(tmp, " ") = 0 Then
tmp = Replace(tmp, ".", ",")
Range(nomTableau).Item(Enreg, c) = CDbl(tmp)
Else
If IsDate(tmp) Then
Range(nomTableau).Item(Enreg, c) = CDate(tmp)
Else
Range(nomTableau).Item(Enreg, c) = tmp
End If
End If
Else
Range(nomTableau).Item(Enreg - 1, c).Copy
Range(nomTableau).Item(Enreg, c).PasteSpecial Paste:=xlPasteFormats
End If
Next c
UserForm_Initialize
raz
End Sub
Private Sub B_précédent_Click()
If Me.Listbox1.ListIndex > 0 Then
Me.Listbox1.ListIndex = Me.Listbox1.ListIndex - 1
End If
End Sub
Private Sub B_suivant_Click()
If Me.Listbox1.ListIndex < Me.Listbox1.ListCount - 1 Then
Me.Listbox1.ListIndex = Me.Listbox1.ListIndex + 1
End If
End Sub