PDA

View Full Version : Excel form - listbox



Mr GT
04-19-2009, 09:57 AM
Hi!

(sorry for my english)

On the form, I have a button to add files to a listbox (the box is bound to a worksheet)

Someone know how to program a button to remove a line from the listbox and the worksheet?


Private Sub BoutonAjoutFichiers_Click()
Dim fichiers As String
FormulaireBackup.CommonDialog1.CancelError = True
On Error GoTo Annulé
FormulaireBackup.CommonDialog1.ShowOpen
fichiers = FormulaireBackup.CommonDialog1.Filename
Worksheets("ListeFichiers").Cells(Range("NoligneFile").Value + 1, 1) = fichiers
BoxListeFichiers.RowSource = "ListeFichiers!A2:A" & Trim(Range("NoligneFile").Value + 1)
Range("NoligneFile").Value = Range("NoligneFile").Value + 1
Annulé:
Exit Sub

End Sub


Thanks! :)

Bob Phillips
04-19-2009, 10:49 AM
If it is bound to the worksheet just delete a line in the worksheet, the listbox will reflect this.

Mr GT
04-19-2009, 10:52 AM
If it is bound to the worksheet just delete a line in the worksheet, the listbox will reflect this.

Hi!

Yes I know, but I want to be able to click an item in the listbox to select it then click the delete button.

I need the code for this delete button. I have no clues on how to program this.

The user won't have access to the worksheet. The user will only see the form.

Bob Phillips
04-19-2009, 11:11 AM
So just find the selected item in the worksheet and delete it. Look at FIND in VBA help.

Mr GT
04-19-2009, 01:13 PM
My solution :


Private Sub BoutonEnleveFichier_Click()
Dim sEffacerFichier As String
Dim R As Range
Dim FindAddress As String

On Error GoTo ChoisirFichier 'Erreur si un utilisateur ne choisit pas de fichier.

sEffacerFichier = BoxListeFichiers.Value

With Worksheets("ListeFichiers").Range("A1:A1048576") 'Spécifie la plage où chercher
Set R = .Find(sEffacerFichier) 'Cherche la première occurence
If Not R Is Nothing Then 'Si une correspondence est trouvé alors
FindAddress = R.Row 'Mettre dans une variable l'adresse de la cellule ou l'occurence est trouvé
Rows(FindAddress).Select
Selection.Delete Shift:=xlUp
End If
End With

Set R = Nothing 'Effacer la mémoire
Range("NoligneFile").Value = Range("NoligneFile").Value - 1
Exit Sub

ChoisirFichier:
MsgBox "SVP, sélectionner un fichier.", vbOKOnly 'Oblige l'utilisateur à spécifier un fichier

End Sub

Bob Phillips
04-19-2009, 01:51 PM
That looks good to me, so well done, hopefully you enjoyed doing that yourself.

Note how I added and around your code, it makes it more readable.

Just one thing you can improve upon. This code



Rows(FindAddress).Select
Selection.Delete Shift:=xlUp


doesn't need the select, you can do



Rows(FindAddress).Delete Shift:=xlUp


I am esepecially impressed that you delete the by the row property, that shows real understanding IMO :)

mdmackillop
04-19-2009, 02:04 PM
BTW
rather than Range("A1:A1048576")
I would use
Worksheets("ListeFichiers").Range("A:A")
or
Worksheets("ListeFichiers").Columns(1)
for the entire column. These methods also give compatability with earlier versions of Excel which did not have that number of rows.

You may also see this method used for the same reason.
Range("A10:A" & Rows.Count)