+ Reply to Thread
Results 1 to 6 of 6

ListBox delete selected if not then message: Please select something from listbox

  1. #1
    Registered User
    Join Date
    12-19-2019
    Location
    Riga, Latvia
    MS-Off Ver
    2019
    Posts
    2

    ListBox delete selected if not then message: Please select something from listbox

    So basically, I have a program in Excel VBA where it shows a listbox with all the users/people and I have a button that deletes a user that is selected
    What I need to do now is: If nothing is selected and I press the delete button It shows a message: You need to select a user then press delete
    Code for the button:

    Private Sub PogaDzest_Click()
    If MsgBox("Do you want to delete selected user?", vbYesNo + vbQuestion) = vbYes Then
    For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
    If Cells(i, 1) = ListBox1.List(ListBox1.ListIndex) Then
    Rows(i).Select
    Selection.Delete
    End If
    Next i
    ListBox1.List(ListBox1.ListIndex) = Delete
    End If


    End Sub

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    19,783

    Re: ListBox delete selected if not then message: Please select something from listbox

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)


    It depends on MultiSelect status of the listbox. If Single then Listindex of -1 denotes no selection.
    If multi or extended you can use the .Selected array
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    2016 primarily
    Posts
    5,575

    Re: ListBox delete selected if not then message: Please select something from listbox

    Please use code tags when posting code.

    Try this:

    Please Login or Register  to view this content.
    Rory
    I drink, and I know things

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,679

    Re: ListBox delete selected if not then message: Please select something from listbox

    When deleting rows you always work from bottom to top.

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  5. #5
    Registered User
    Join Date
    12-19-2019
    Location
    Riga, Latvia
    MS-Off Ver
    2019
    Posts
    2

    Re: ListBox delete selected if not then message: Please select something from listbox

    Thank you this worked, also what do you mean by code tags? What are the tags I added at the bottom? button, code, listbox, vba , these do not count as code tags? First timer here, sorry for the inconvinience ill know from now.

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,679

    Re: ListBox delete selected if not then message: Please select something from listbox

    To add Code Tags.

    Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, as a relatively new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Moving all columns from listbox 1 to listbox 2 and see all details of an selected item
    By alexgoaga in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-05-2018, 07:09 PM
  2. Replies: 7
    Last Post: 01-29-2017, 03:22 PM
  3. Show dynamic values at listbox depending value selected another listbox
    By Judith_Chao in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-25-2017, 08:30 AM
  4. [SOLVED] if anything in listbox select first item, if listbox empty do nothing (listbox in userform
    By mcdermott2 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-17-2015, 12:49 PM
  5. Copy Selected items from multicolumn, multiselect listbox to another listbox
    By Willigb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-19-2013, 11:27 AM
  6. VBA - Populate Listbox based on value selected in another ListBox (On Userform)
    By raaboo in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-12-2012, 11:18 AM
  7. Populate userform listbox based on value selected in another listbox
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-07-2012, 05:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1