VBA: Delete row using ID number and input box????

  • Hi guys. I'd appreciate any help on this.


    I have a basic spreadsheet with the first column being 'ID'. I want to be able to delete any row with data in by using VBA to code a macro that when ran by a button will open an input box - "Enter ID number of row you want to delete". The ID number will be entered and all data from that row will be deleted and the rest of the rows will move up to fill the space.


    Any ideas.


    Thanks


    Anthony

  • Hi Anthony,


    Assuming the IDs are in Column A, try this... Just be aware that it will delete the entire row of the worksheet when it finds a match.


    Code
    1. Sub Delete_ID()
    2. Dim stIDSelect as String
    3. Dim c As Range
    4. Let stIDSelect = InputBox("Enter ID number of row you want to delete")
    5. If stIDSelect = vbNullString Then Exit Sub
    6. For Each c In Range("A:A")
    7. If c = stIDSelect Then c.EntireRow.Delete
    8. Next
    9. End Sub

    If I've been helpful, let me know. If I haven't, let me know that too.

  • Anthony,


    It's probably not finding a match because the Inputbox entry is a string and your IDs are numbers.


    Try this code...

    If I've been helpful, let me know. If I haven't, let me know that too.

  • Re: VBA: Delete row using ID number and input box????


    Anthony,


    Sorry, I didn't notice your last inquiry until today.


    Try using this, adjusting the column reference as needed.

    If I've been helpful, let me know. If I haven't, let me know that too.