Consulting

Results 1 to 12 of 12

Thread: Multi-Delete Function Problem in ListBox1

  1. #1

    Multi-Delete Function Problem in ListBox1

    Hi ~ ALL

    This vba can Open the Files with multiselect function and the files path will show in the listbox. Also, it can delete the single file path in list box.
    [VBA]
    ? Multi-select the file(s) and list in the listbox1
    Private Sub cmdInput_Click()
    Dim Files As Variant
    Dim sFileShort As String
    Dim i As Long
    Dim Title As String
    Dim Finfo As String
    Dim Msg As String

    ' Setup lists of file filters
    Finfo = "Exceld Files (*.xls),*.xls,"


    ' Set the dialog box caption
    Title = "select a File to Import"
    Files = Application.GetOpenFilename(Finfo, , Title, MultiSelect:=True)

    ' Exit if dialog box canceled
    If Not IsArray(Files) Then
    MsgBox "No file was selected."
    Exit Sub
    End If

    'Display full path and name of the files
    For i = LBound(Files) To UBound(Files)
    FileShort = Right(Files(i), Len(Files(i)))
    With Me.ListBox1
    .AddItem sFileShort
    .List(.ListCount - 1, 1) = Files(i)
    End With
    Next i
    End Sub

    ' Delete the single file
    Private Sub cmdDelete_Click()
    Me.ListBox1.RemoveItem (Me.ListBox1.ListIndex)
    End Sub[/VBA]

    Then, i try to modify it with the Multi-Delete function. Although it can multi-select the files in listbox, the action stills delete single file when i click the delete button.
    [VBA]
    With Me.ListBox1
    .MultiSelect = 2
    .AddItem sFileShort
    .List(.ListCount - 1, 1) = Files(i)
    End With
    [/VBA]
    Therefore, how to modify it with multi-delete function. Thanks

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub cmdDeelete_Click()
    Dim i As Long
    With Me.ListBox1
    For i = .ListCount - 1 To 0 Step -1
    If .Selected(i) Then
    .RemoveItem i
    End If
    Next i
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    According the lists, When i select the bottom of list of files, it deletes all the files in listbox. Also. it cannot delete 2 or above files in once.

    Thanks

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Pardon?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    With your suggested VBA command, it can't multi-select the files in listbox. Also, it will delete all files in listbox when i selected the bottom file in listbox. I want to delete 2 or above files when i click "delete button" once(multi-delete). Delete the number of files which depend on the user like. I want to multi-select files in listbox which can be multi-deleted the selected files.

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    No it doesn't, it deletes whatever items in the listbox that you have selected, as you were showing (albeit for a singleton item) in your example.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    if you use multiselect (i used check boxes)
    this code will delete all the selcted/checked items in the listbox
    all at once without prompt
    you may have to add a handler for nothing selected.

    might be some help, I am not too sure

    i would definitely add a single prompt at the end to ensure your not removing needed sheets

    cheers

    ---------------------------------------------
    For i = 0 To ListBox1.ListCount
    While ListBox1.Selected(i) = True
    Application.DisplayAlerts = False
    ThisWorkbook.Sheets.Select (i)
    ThisWorkbook.Sheets(i).Delete
    UserForm1.ListBox1.RemoveItem (i)
    Wend
    next

  8. #8
    It means that it can't multi-select the selected items in listbox which can multi-delete the selected items in my case.

    If refer to daniel suggestion, how to add a single prompt in the case.
    Thanks

  9. #9
    not sure, its up to you really,
    maybe just a msgbox displaying the items to be removed,
    just to be safe, so the user can check the selection again before final removal

  10. #10
    Hello all !!!
    I can do that the purpose of multi-select the selected items in listbox which can multi-delete the selected items . Or I am very tired in last night. Xld suggest the vba command is works and plus my modified vba command.Here it is:
    [VBA]'Display full path and name of the files
    For i = LBound(sFiles) To UBound(sFiles)
    sFileShort = Right(sFiles(i), Len(sFiles(i)))
    With Me.ListBox1
    .MultiSelect = 2
    .AddItem sFileShort
    .List(.ListCount - 1, 1) = sFiles(i)
    End With
    Next i

    Private Sub cmdDelete_Click()
    Dim k As Long
    With Me.ListBox1
    For k = .ListCount - 1 To 0 Step -1
    If .Selected(k) Then
    .RemoveItem k
    End If
    Next k
    End With
    End Sub[/VBA]

    Anyway thanks xld and daniel

  11. #11

    How to modify the below vba that can all open the filelist in the listbox

    How to modify the below vba that can all open the filelist in the listbox at once?
    [VBA]Private Sub cmdRun_Click()
    Workbooks.Open Filename:=Me.ListBox1.List(Me.ListBox1.ListIndex)
    End Sub[/VBA]

    Thanks

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub cmdRun_Click()
    Dim i As Long
    With Me.ListBox1
    For i = 0 To .ListCount
    Workbooks.Open Filename:=.List(i)
    Next i
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •