Bonjour,
Nous allons utiliser la propriété TabIndex des TextBox.
Ouvrez votre base de données et comptez le nombre de colonnes
Ensuite allez dans l'éditeur(Alt F11) et insérez un UserForm. Mettez autant de TextBox que de colonnes comme ceci:
Mettez les Commandbutton:
Puis allez dans la partie code et copier coller ceci:
Option Explicit Dim Ctrl As Control Dim DerniereLigneUtilisee As Long Dim x As Integer Private Sub afficher(ByVal ligne As Integer) For Each Ctrl In Controls If TypeName(Ctrl) = "TextBox" Then Ctrl.Object.Value = Cells(ligne, Ctrl.TabIndex + 1) Ctrl.ControlTipText = Cells(1, Ctrl.TabIndex + 1) End If Next Ctrl End Sub 'premier de la liste Private Sub CommandButton1_Click() Cells(2, 1).Select afficher (ActiveCell.Row) CommandButton4.Enabled = True CommandButton2.Enabled = True End Sub 'dernier de la liste Private Sub CommandButton2_Click() Selection.End(xlDown).Select afficher (ActiveCell.Row) CommandButton3.Enabled = True CommandButton2.Enabled = False End Sub 'précédent Private Sub CommandButton3_Click() If ActiveCell.Row = 2 Then CommandButton3.Enabled = False Exit Sub Else Application.ScreenUpdating = False If Selection.Row > 1 Then Selection.Offset(-1).Select End If afficher (ActiveCell.Row) CommandButton3.Enabled = True CommandButton2.Enabled = True End If End Sub 'Suivant Private Sub CommandButton4_Click() If ActiveCell.Offset(1, 1).Value = "" Then CommandButton4.Enabled = False Exit Sub Else Application.ScreenUpdating = False Selection.Offset(1).Select afficher (ActiveCell.Row) CommandButton3.Enabled = True End If End Sub 'ajouter une ligne Private Sub CommandButton5_Click() Application.ScreenUpdating = False Sheets("Customer").Activate 'adapter le nom de la feuille DerniereLigneUtilisee = Range("A" & Rows.Count).End(xlUp).Row + 1 For Each Ctrl In Controls If TypeName(Ctrl) = "TextBox" Then Cells(DerniereLigneUtilisee, Ctrl.TabIndex + 1) = Ctrl.Object.Value End If Next Ctrl For x = 1 To ActiveSheet.UsedRange.Columns.Count 'redimensionne les colonnes Columns(x).EntireColumn.AutoFit Next x End Sub 'supprimer une ligne Private Sub CommandButton6_Click() If ActiveCell.Value = "" Then Exit Sub Rows(ActiveCell.Row & ":" & ActiveCell.Row).Delete Shift:=xlUp End Sub 'modifier une ligne Private Sub CommandButton7_Click() If ActiveCell.Value = "" Then Exit Sub Application.ScreenUpdating = False Sheets("Customer").Activate 'adapter le nom de la feuille DerniereLigneUtilisee = Range("A" & Rows.Count).End(xlUp).Row + 1 For Each Ctrl In Controls If TypeName(Ctrl) = "TextBox" Then Cells(ActiveCell.Row, Ctrl.TabIndex + 1) = Ctrl.Object.Value End If Next Ctrl For x = 1 To ActiveSheet.UsedRange.Columns.Count 'redimensionne les colonnes Columns(x).EntireColumn.AutoFit Next x End Sub 'enregistrer dans le feuille SaveData Private Sub CommandButton8_Click() If ActiveCell.Value = "" Then Exit Sub Application.ScreenUpdating = False Sheets("SaveData").Activate 'adapter le nom de la feuille DerniereLigneUtilisee = Range("A" & Rows.Count).End(xlUp).Row + 1 For Each Ctrl In Controls If TypeName(Ctrl) = "TextBox" Then Cells(DerniereLigneUtilisee, Ctrl.TabIndex + 1) = Ctrl.Object.Value End If Next Ctrl For x = 1 To ActiveSheet.UsedRange.Columns.Count 'redimensionne les colonnes Columns(x).EntireColumn.AutoFit Next x Sheets("Customer").Activate 'adapter le nom de la feuille End Sub Private Sub UserForm_Initialize() Sheets("Customer").Activate 'adapter le nom de la feuille Range("A2").Select afficher (2) CommandButton1.Caption = "Premier" CommandButton2.Caption = "Dernier" CommandButton3.Caption = "Précédent" CommandButton4.Caption = "Suivant" CommandButton5.Caption = "Ajouter" CommandButton6.Caption = "Supprimer" CommandButton7.Caption = "Modifier" CommandButton8.Caption = "Enregistrer" End Sub
Voilà le résultat:
Bonne programmation