Ajout d'une ligne en fin de tableau
Bonjour le Forum,
J'essaie de réaliser une macro apparemment assez basique mais il doit me manquer un truc...
Sub test()
With Range("Tableau1").ListObject
'ajout d'une ligne vierge à la fin du tableau
.ListRows.Add
End With
End Sub
Lors de la première exécution de la macro, rien ne se passe.
L'ajout de ligne ne se fait qu'à partir de la 2e exécution.
l'un d'entre vous pourrait m'éclairer?
Merci d'avance et bonne journée.
Bonjour
La ligne actuelle totalement vide n'est pas pas un listrow (et d'alleurs à ce stade DataBodyRange is nothing) : le code en fait un listrow
Ok, merci pour cette précision 78chris.
Bonjour le Forum,
Dans la continuité de ce post :
Afin de renseigner un journal de modification, j'utilisai ce code fourni par Steelson
Private Sub Workbook_Sheetchange(ByVal feuille As Object, ByVal cible As Range)
Dim avant() As Variant, apres() As Variant
On Error GoTo fin
If feuille.Name = "Journal" Then Exit Sub
If cible.Columns.Count = Columns.Count Or cible.Rows.Count = Rows.Count Then
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
MsgBox "Impossible d'agir sur une ligne ou une colonne complète !"
Exit Sub
End If
Application.EnableEvents = False
ReDim avant(1 To cible.Rows.Count, 1 To cible.Columns.Count)
ReDim apres(1 To cible.Rows.Count, 1 To cible.Columns.Count)
Application.Undo
For lig = 1 To UBound(avant)
For col = 1 To UBound(avant, 2)
avant(lig, col) = cible.Cells(1, 1).Offset(lig - 1, col - 1).FormulaLocal
Next
Next
Application.Undo
For lig = 1 To UBound(apres)
For col = 1 To UBound(apres, 2)
apres(lig, col) = cible.Cells(1, 1).Offset(lig - 1, col - 1).FormulaLocal
Next
Next
For lig = 1 To cible.Rows.Count
For col = 1 To cible.Columns.Count
If avant(lig, col) <> apres(lig, col) Then
With Sheets("Journal").ListObjects(1)
.ListRows.Add
i = .ListRows.Count
With .DataBodyRange
.Cells(i, 1) = feuille.Cells(1, cible.Cells(1, 1).Offset(lig - 1, col - 1).Column)
.Cells(i, 2) = Date
.Cells(i, 3) = "'" & avant(lig, col)
.Cells(i, 4) = "'" & apres(lig, col)
.Cells(i, 5) = Environ("username")
End With
End With
End If
Next
Next
fin:
Application.EnableEvents = True
If Err Then MsgBox "Erreur #" & Err.Number & " !"
End Sub
Mais actuellement, je modifie mes valeurs via un Userform et du coup il n'est plus adapté
En fouillant un peu, j'ai réussi à faire ceci :
Private Sub CommandButton1_Click()
If Me.TextBox1.Value = "" Then
GoTo Line1
Else
With Worksheets("Journal")
.ListObjects("Tableau2").ListRows.Add
.Range("A" & Rows.Count).End(xlUp) = ComboBox1.Value
.Range("B" & Rows.Count).End(xlUp) = Label2.Caption
.Range("C" & Rows.Count).End(xlUp) = Date
.Range("D" & Rows.Count).End(xlUp) = Application.Index(Range("Tableau1[#All]"), Application.Match(ComboBox1.Value, Range("Tableau1[[#All],[Produit]]"), 0), Application.Match(Label2.Caption, Range("Tableau1[#Headers]"), 0))
.Range("E" & Rows.Count).End(xlUp) = TextBox1.Value
.Range("F" & Rows.Count).End(xlUp) = Application.UserName
End With
Application.Index(Range("Tableau1[#All]"), Application.Match(ComboBox1.Value, Range("Tableau1[[#All],[Produit]]"), 0), Application.Match(Label2.Caption, Range("Tableau1[#Headers]"), 0)) = TextBox1.Value
End If
Line1:
If Me.TextBox2.Value = "" Then
GoTo Line2
Else
With Worksheets("Journal")
.ListObjects("Tableau2").ListRows.Add
.Range("A" & Rows.Count).End(xlUp) = ComboBox1.Value
.Range("B" & Rows.Count).End(xlUp) = Label3.Caption
.Range("C" & Rows.Count).End(xlUp) = Date
.Range("D" & Rows.Count).End(xlUp) = Application.Index(Range("Tableau1[#All]"), Application.Match(ComboBox1.Value, Range("Tableau1[[#All],[Produit]]"), 0), Application.Match(Label3.Caption, Range("Tableau1[#Headers]"), 0))
.Range("E" & Rows.Count).End(xlUp) = TextBox2.Value
.Range("F" & Rows.Count).End(xlUp) = Application.UserName
End With
Application.Index(Range("Tableau1[#All]"), Application.Match(ComboBox1.Value, Range("Tableau1[[#All],[Produit]]"), 0), Application.Match(Label3.Caption, Range("Tableau1[#Headers]"), 0)) = TextBox2.Value
End If
Line2:
If Me.TextBox3.Value = "" Then
GoTo fin
Else
With Worksheets("Journal")
.ListObjects("Tableau2").ListRows.Add
.Range("A" & Rows.Count).End(xlUp) = ComboBox1.Value
.Range("B" & Rows.Count).End(xlUp) = Label4.Caption
.Range("C" & Rows.Count).End(xlUp) = Date
.Range("D" & Rows.Count).End(xlUp) = Application.Index(Range("Tableau1[#All]"), Application.Match(ComboBox1.Value, Range("Tableau1[[#All],[Produit]]"), 0), Application.Match(Label4.Caption, Range("Tableau1[#Headers]"), 0))
.Range("E" & Rows.Count).End(xlUp) = TextBox3.Value
.Range("F" & Rows.Count).End(xlUp) = Application.UserName
End With
Application.Index(Range("Tableau1[#All]"), Application.Match(ComboBox1.Value, Range("Tableau1[[#All],[Produit]]"), 0), Application.Match(Label4.Caption, Range("Tableau1[#Headers]"), 0)) = TextBox3.Value
End If
fin:
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer, j As Integer, R As Range
For Each R In Range("Tableau1[Produit]")
ComboBox1.AddItem R
Next
End Sub
Mais cela me parait lourd (surtout si je rajoute des TextBox)
Y a-t-il un moyen "d'alléger" ce code? ou de modifié le premier pour qu'il s'applique à un remplissage via Userform?
J'ai oublié le fichier qui va avec...