+ Reply to Thread
Results 1 to 9 of 9

Not deleting the selected data from listbox by one click on command button

  1. #1
    Registered User
    Join Date
    05-16-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    35

    Not deleting the selected data from listbox by one click on command button

    I even gave it a range to delete, however, i have to click the delete button twice to delete what is entered. First click merges the 2 columns and 2nd click deletes.
    I want to only delete what i have selected from the listbox with one click of the command button. Listbox has 2 columns. I am using the code below


    Please Login or Register  to view this content.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Not deleting the selected data from listbox by one click on command button

    What do you want to happen? Do you really want a "delete," which will delete the value and move other cells to fill the deleted cell? When you don't explicitly give an argument for Delete, then Excel decides for you how best to do the fill. You said "First click merges the 2 columns..." What 2 columns? I don't see anything here that would merge columns at all.

    Two other points. I would recommend declaring all variables; c1 should be declared as a Range. Also, your With...End With is superfluous. The code does not take advantage of it so it's not clear what you had in mind.

    It might helpful to attach your entire workbook instead of just this snippet of code, which is difficult to diagnose out of context. It would be helpful to see what is in your listbox.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    05-16-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Not deleting the selected data from listbox by one click on command button

    Quote Originally Posted by 6StringJazzer View Post
    What do you want to happen? Do you really want a "delete," which will delete the value and move other cells to fill the deleted cell? When you don't explicitly give an argument for Delete, then Excel decides for you how best to do the fill. You said "First click merges the 2 columns..." What 2 columns? I don't see anything here that would merge columns at all.

    Two other points. I would recommend declaring all variables; c1 should be declared as a Range. Also, your With...End With is superfluous. The code does not take advantage of it so it's not clear what you had in mind.

    It might helpful to attach your entire workbook instead of just this snippet of code, which is difficult to diagnose out of context. It would be helpful to see what is in your listbox.
    Hi 6StringJazze, thank you for your email. Here is the template

    When i enter values in the 2 textboxes and press Add, it will show on the listbox and spreadsheet. Now when i make a selection from the listbox and click delete, it deletes one by one, meaning it would delete the number first and then i would have to click on delete again to delete the name.
    Attached Files Attached Files

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Not deleting the selected data from listbox by one click on command button

    Try this code. Your code only deletes a single cell. I added the EntireRow method.

    I also suggest that when building a form, you change the default names of the controls. There is nothing in your code to indicate that CommandButton4 is the Delete button, so I added a comment. But you can name the button something like ButtonDelete. Even if nobody else ever reads your code, your future self will thank you if you ever have to modify the code.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    05-16-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Not deleting the selected data from listbox by one click on command button

    Thanks 6StringJazzer, i tried this but i dont want to delete the data in other columns (A,B,C,D,E,F) . This code you provided deletes the entirerow including data that i have in other columns

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Not deleting the selected data from listbox by one click on command button

    You could try this.
    Please Login or Register  to view this content.
    Or if you are only ever deleting one item from the list.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  7. #7
    Registered User
    Join Date
    05-16-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Not deleting the selected data from listbox by one click on command button

    Thanks Norie, that works good but how come when i have 2 row with equal value, it deletes them both and sometimes it deletes others aswell. I mean when i have this data

    G H
    2 2
    2 2
    1 1
    4 4
    1 2


    now if i select the 2nd row to delete, it would delete the 1st row aswell. When i select the last row it would delete the last + the 2 above

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Not deleting the selected data from listbox by one click on command button

    Which code did you use?

    If you used the loop it's going to delete everything it finds that matches the selection in the listbox.

    If you use the second code I posted it will only delete the item selected in the listbox, it won't delete any duplicate items.

  9. #9
    Registered User
    Join Date
    05-16-2013
    Location
    Canada
    MS-Off Ver
    Excel 2003
    Posts
    35

    Re: Not deleting the selected data from listbox by one click on command button

    Thanks Norie

+ 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] Command button grayed out till option button selected
    By Jeff121 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-25-2013, 11:00 AM
  2. selected sheet gets opend on command click
    By kshitij_dch in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-21-2012, 11:15 AM
  3. Using a command button to copy selected data to another worksheet
    By 02.flea in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-17-2012, 02:12 AM
  4. Replies: 2
    Last Post: 06-11-2012, 05:14 PM
  5. [SOLVED] Need selected item from listbox after double click
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-23-2006, 11:20 AM

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