Modification via un userform et listview
Bonjour tout le monde,
Voici mon problème.
J'ai une feuille avec le listing des sortie d'inventaire et un userform pour le contrôler.
Dans le userform je ne veut pas écrire une nouvelle ligne.
J'affiche le tout dans une listview et lors d'un clique sur une ligne, je remplis les textbox avec l'information de ceux-ci.
J'ai deux option après, Suppression qui marche bien et modification.
Quand j'essaie de faire une modification c'est aléatoire, un coup la modification est fait mais sinon j'ai une nouvelle ligne qui ce créer.
Option Explicit
Dim Ws As Worksheet
Private Sub UserForm_Initialize()
Alimente_Listview
End Sub
'------------------------------------------------------------------------------------------------------------
Private Sub CommandButton30_Click() 'Actions produitent après un clic sur le bouton "Ajouter/modifier"
Dim Cel As Range
Dim Ligne As Long
Set Ws = Worksheets("Log_Sortie")
' If Ws.Name = "Recherche" Then Exit Sub
If Trim(Me.TextBox1) = "" Then
MsgBox "Référence obligatoire" 'Message d'erreur Référence absente
Exit Sub
End If
Set Cel = Ws.Columns("A").Find(what:=Me.TextBox1, LookIn:=xlValues, lookat:=xlWhole)
If Not Cel Is Nothing Then
If MsgBox("Voulez-vous modifier cet enregistrement ?", vbQuestion + vbYesNo + vbDefaultButton2, _
"Modification") <> vbYes Then Exit Sub
Ligne = Cel.Row
Else
Ws.Rows(3).Insert
Ligne = 3
End If
Ws.Range("A" & Ligne) = TextBox1
Ws.Range("B" & Ligne) = TextBox2
Ws.Range("C" & Ligne) = TextBox3
Ws.Range("D" & Ligne) = TextBox4
Ws.Range("E" & Ligne) = TextBox5
Ws.Range("F" & Ligne) = TextBox6
Ws.Range("G" & Ligne) = TextBox7
Ws.Range("H" & Ligne) = TextBox8
Ws.Range("I" & Ligne) = TextBox9
Ws.Range("J" & Ligne) = TextBox10
Ws.Range("K" & Ligne) = TextBox11
Ws.Range("L" & Ligne) = TextBox12
Ws.Range("M" & Ligne) = TextBox13
Ws.Range("N" & Ligne) = TextBox14
Ws.Range("O" & Ligne) = TextBox15
Ws.Range("P" & Ligne) = TextBox16
Ws.Range("Q" & Ligne) = TextBox17
Ws.Range("R" & Ligne) = Now
Ws.Range("R" & Ligne) = Environ("Username")
Alimente_Listview
Me.TextBox3.SetFocus 'Place le curseur sur la Textbox1
'ActiveWorkbook.Save 'Sauvegarde du classeur
End Sub
'--------------------------------------------------------------------------------------------------
Private Sub CommandButton2_Click() ' Supprimer
' Suppression
Dim Cel As Range
Set Ws = Worksheets("Log_Sortie")
'If Ws.Name = "Recherche" Then Exit Sub
If Trim(Me.TextBox1) = "" Then
MsgBox "Pas de Référence"
Exit Sub
End If
Set Cel = Ws.Columns("A").Find(what:=Me.TextBox1, LookIn:=xlValues, lookat:=xlWhole)
If Not Cel Is Nothing Then
If MsgBox("Voulez-vous supprimer cet enregistrement ?", vbQuestion + vbYesNo + vbDefaultButton2, _
"Opération irréversible") <> vbYes Then Exit Sub
Ws.Rows(Cel.Row).Delete
End If
Alimente_Listview
Me.TextBox1.SetFocus 'Place le curseur sur la Textbox1
'ActiveWorkbook.Save 'Sauvegarde du classeur
End Sub
'--------------------------------------------------------------------------------------------------------------
Private Sub ListView1_Click()
'Lecture d'un élément
Dim i As Integer
Dim J As Integer
i = Me.ListView1.SelectedItem.Index
Me.TextBox1 = Me.ListView1.ListItems(i).Text
For J = 1 To Me.ListView1.ColumnHeaders.Count - 1
Me.Controls("Textbox" & J + 1) = ListView1.ListItems(i).ListSubItems(J).Text
Next J
End Sub
'-------------------------------------------------------------------------------------------------------------------------
Sub Alimente_Listview()
' Remplissage
Dim C As Long
Dim i As Long
Dim FilterRow As Range
Dim FilterRng As Range
Dim N As Long
Dim rng As Range
Dim Total As Currency
Dim Wks As Worksheet
Dim Ws As Worksheet
For i = 1 To 18
Me.Controls("TextBox" & i) = ""
Next i
Set Ws = Worksheets("Log_Sortie")
Set Wks = Worksheets("Log_Sortie")
Set rng = Wks.Range("A1").CurrentRegion
Set FilterRng = rng.SpecialCells(xlCellTypeVisible)
Set FilterRng = Intersect(FilterRng, rng.Offset(1, 0))
'Configure the appearance of the ListView object
ListView1.ListItems.Clear
ListView1.ColumnHeaders.Clear
ListView1.Gridlines = True
ListView1.FullRowSelect = True
'Add the Column Headers
For i = 1 To rng.Columns.Count
ListView1.ColumnHeaders.Add i, , rng.Item(1, i)
Next i
If FilterRng Is Nothing Then Exit Sub
'Fill in the Rows and Columns with data from the table
With ListView1
For Each FilterRow In FilterRng.Rows
'Add a row - This also used to references the first column
With ListView1.ListItems.Add
'SubItem (other columns in the row) counter
i = 1
'Fill column 1
.Text = FilterRow.Cells(1, 1)
'Fill the remaining columns
For C = 2 To FilterRow.Cells.Count
.SubItems(i) = FilterRow.Cells(1, C).Text
i = i + 1
Next C
End With
Next FilterRow
End With
End Sub
'-----------------------------------------------------------------------------------------------------------------------
Voir pièces jointe
Le problème vient t'il du fait que j'utilise la date de l'écriture dans le journal comme référence de recheche....
bonjour
ses pas mal mes je préfère séparer la listview de la modife
1 userform pour la listvien
2 userform pour la createmodif
voila un modèle
A+
Maurice
Bonjour bonjour,
Voici ce que j'ai fait avec l'aide qu'il m'a été donné.
Je le mets en pièce jointe pour aider les gens qui voudraient s'en servir.
Merci Archer j'ai un peut modifier ce que tu m’as offert.
Ci certain ont des idées je suis ouvert à faire des modifications.
archer a écrit :bonjour
ses pas mal mes je préfère séparer la listview de la modife
1 userform pour la listvien
2 userform pour la createmodif
voila un modèle
A+
Maurice
Merci
Projet ouvert à toussent.
https://forum.excel-pratique.com/excel/projet-inventaire-applicative-t101218.html