I reto avy ny *code VBA fototra* ho an'ny *Excel UserForm*
ahafahana manao :
- *Ajouter* (manampy donnée)
- *Supprimer* (mamafa donnée)
- *Modifier* (manova donnée)
- *Rechercher* (mitady donnée)
Atao hoe manana *UserForm* ianao (ex: `UserForm1`) misy
champ toy izao :
- `txtNom` (anarana)
- `txtPrenom` (fanampiny)
- `txtTel` (finday)
- `ListBox1` (fampisehoana liste)
*1. Ajouter :*
```vba
Private Sub btnAjouter_Click()
Dim ligne As Long
ligne = Sheets("Feuil1").Cells([Link], 1).End(xlUp).Row + 1
With Sheets("Feuil1")
.Cells(ligne, 1).Value = [Link]
.Cells(ligne, 2).Value = [Link]
.Cells(ligne, 3).Value = [Link]
End With
Call ActualiserListe
End Sub
```
*2. Supprimer :*
```vba
Private Sub btnSupprimer_Click()
If [Link] = -1 Then Exit Sub
Dim ligne As Long
ligne = [Link] + 2
Sheets("Feuil1").Rows(ligne).Delete
Call ActualiserListe
End Sub
```
*3. Modifier :*
```vba
Private Sub btnModifier_Click()
If [Link] = -1 Then Exit Sub
Dim ligne As Long
ligne = [Link] + 2
With Sheets("Feuil1")
.Cells(ligne, 1).Value = [Link]
.Cells(ligne, 2).Value = [Link]
.Cells(ligne, 3).Value = [Link]
Ity ambany ity ny *bouton "Recherche"* feno ho an’ny *UserForm*
amin’ny Excel VBA, izay mitady anarana iray ao amin'ny *Feuil1*,
puis mampiseho ny mombamomba azy amin'ny champ hafa:
*Code ho an'ny bouton “Rechercher” :*
```vba
Private Sub btnRecherche_Click()
Dim i As Long, trouve As Boolean
Dim nomRecherche As String
nomRecherche = [Link]
For i = 2 To Sheets("Feuil1").Cells([Link], 1).End(xlUp).Row
If Sheets("Feuil1").Cells(i, 1).Value = nomRecherche Then
[Link] = Sheets("Feuil1").Cells(i, 2).Value
[Link] = Sheets("Feuil1").Cells(i, 3).Value
trouve = True
Exit For
End If
Next i
If Not trouve Then
MsgBox "Tsy hita ilay anarana", vbExclamation
[Link] = ""
[Link] = ""
End If
End Sub
```
*Fepetra:*
- `txtNom` = ilay champ fidirana anarana tadiavina.
- `txtPrenom` sy `txtTel` = ilay hisehoan’ny valiny.
- Azonao ovaina araka ny anaran’ny textbox-nao.
Lazao raha mila ho *recherche partielle* (oh: mitady anarana
manomboka amin’ny litera iray), dia omeko code hafa.