+ Reply to Thread
Results 1 to 15 of 15

VBA to delete rows based on user input

Hybrid View

  1. #1
    Registered User
    Join Date
    05-17-2020
    Location
    Philippines
    MS-Off Ver
    2016
    Posts
    35

    VBA to delete rows based on user input

    Apologies, I was not aware of the tags. I am not sure if I did it correctly but I did as I understood from your instructions.
    Thank you so much.

    Hi!

    I have this code to delete an employee based on the employee id a user inputs. I placed a note on the message box because I cannot figure out how I can delete several employee id inputs all at once.

    Can anybody help me add a command to delete multiple rows based on user input of employee id's (separated by semicolon)? It would also be cool to have a confirmation message like:

    The following employee id's were deleted from the list:
    123
    456
    789

    The following employee id's were not found:
    654
    321



    Sub delete_employee()
    Dim lastRow As Long, x As Long
    Dim resp
    resp = InputBox("Enter Id of employee to delete." & vbLf & "NOTE: This action can only be done one employee id at a time.", "Delete")
    If resp = "" Then Exit Sub
    MsgBox ("Are you sure you want to permanently delete this employee from the list?" & vbLf & resp)
    lastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
    If IsDate(resp) Then
        resp = CDate(resp)
    End If
    For x = lastRow To 1 Step -1
        If IsDate(resp) Then
        If Cells(x, "A").Value = resp Then
            Rows(x).Delete
        End If
        End If
    If Not IsDate(resp) Then
        If UCase(Cells(x, "A").Value) = UCase(resp) Then
            Rows(x).Delete
        End If
        End If
    Next
    End Sub

    Thank you so much for all your help!
    Last edited by Roux11; 05-25-2020 at 04:00 AM.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2505 Win 11
    Posts
    24,720

    Re: VBA to delete rows based on user input

    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.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: VBA to delete rows based on user input

    Hello Roux11,


    Try the below code

    Sub DeleteEmployees()
    
    Dim IDs As String, Del As String, NotDel As String
    IDs = InputBox("Enter employee ID. If you have more than one ID, seperat them by a semicolon", "Delete IDs")
    If IDs = vbNullString Then MsgBox "No IDs were selected !", vbExclamation: Exit Sub
    
    If InStr(IDs, ";") Then
       For x = 0 To UBound(Split(IDs, ";"))
          If Columns(1).Find(Split(IDs, ";")(x), lookat:=xlWhole) Is Nothing Then
             NotDel = IIf(Len(NotDel) > 0, NotDel & vbLf & Split(IDs, ";")(x), Split(IDs, ";")(x))
          Else
             Columns(1).Find(Split(IDs, ";")(x), lookat:=xlWhole).EntireRow.Delete
             Del = IIf(Len(NotDel) > 0, Del & vbLf & Split(IDs, ";")(x), Split(IDs, ";")(x))
          End If
       Next
    Else
       If Columns(1).Find(IDs, lookat:=xlWhole) Is Nothing Then NotDel = IDs Else Del = IDs: Columns(1).Find(IDs, lookat:=xlWhole).EntireRow.Delete
    End If
    
    MsgBox "Task completed" & vbLf & vbLf & "ID(s) deleted :" & vbLf & Del & vbLf & vbLf & "ID(s) NOT deleted" & vbLf & NotDel, vbInformation
    
    End Sub
    Last edited by nankw83; 05-25-2020 at 06:11 AM. Reason: Adjusted code to include deleted IDs as well in the MsgBox
    If I was able to help, you can thank me by clicking the * Add Reputation under my user name

  4. #4
    Registered User
    Join Date
    05-17-2020
    Location
    Philippines
    MS-Off Ver
    2016
    Posts
    35

    Re: VBA to delete rows based on user input

    Thank you so much! This is perfect!

  5. #5
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: VBA to delete rows based on user input

    Glad to help & thanks for reporting back. If the suggested solution takes care of your request, please mark this thread as 'SOLVED' using Thread Tools from the top menu & thanks for the reps

  6. #6
    Registered User
    Join Date
    05-17-2020
    Location
    Philippines
    MS-Off Ver
    2016
    Posts
    35

    Re: VBA to delete rows based on user input

    Sure. Can I request one more? After entering the employee ids then clicking OK, can another message box appear with a question 'Are you sure you want to permanently delete these employees from the roster? then an OK and CANCEL button?

  7. #7
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: VBA to delete rows based on user input

    Sure ... Line added in red below

    Sub DeleteEmployees()
    
    Dim IDs As String, Del As String, NotDel As String
    IDs = InputBox("Enter employee ID. If you have more than one ID, seperat them by a semicolon", "Delete IDs")
    If IDs = vbNullString Then MsgBox "No IDs were selected !", vbExclamation: Exit Sub
    If (MsgBox("Are you sure you want to permanently delete these employees from the roster?", vbExclamation + vbOKCancel)) = vbCancel Then Exit Sub
    
    If InStr(IDs, ";") Then
       For x = 0 To UBound(Split(IDs, ";"))
          If Columns(1).Find(Split(IDs, ";")(x), lookat:=xlWhole) Is Nothing Then
             NotDel = IIf(Len(NotDel) > 0, NotDel & vbLf & Split(IDs, ";")(x), Split(IDs, ";")(x))
          Else
             Columns(1).Find(Split(IDs, ";")(x), lookat:=xlWhole).EntireRow.Delete
             Del = IIf(Len(NotDel) > 0, Del & vbLf & Split(IDs, ";")(x), Split(IDs, ";")(x))
          End If
       Next
    Else
       If Columns(1).Find(IDs, lookat:=xlWhole) Is Nothing Then NotDel = IDs Else Del = IDs: Columns(1).Find(IDs, lookat:=xlWhole).EntireRow.Delete
    End If
    
    MsgBox "Task completed" & vbLf & vbLf & "ID(s) deleted :" & vbLf & Del & vbLf & vbLf & "ID(s) NOT deleted" & vbLf & NotDel, vbInformation
    
    End Sub

  8. #8
    Registered User
    Join Date
    05-17-2020
    Location
    Philippines
    MS-Off Ver
    2016
    Posts
    35

    Re: VBA to delete rows based on user input

    It is working fine, thank you but I have a code interfering with the delete process which is the application.enable events=true

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Dim cel As Range
    Dim myRow As Long
    
    Set ws = ThisWorkbook.Sheets("Termed")
    
    If Not Intersect(Target, Range("A4:A13")) Is Nothing Then ' watch all the cells in this range
        For Each cel In Target ' do the next steps for each cell that was changed
            myRow = cel.Row
            Range("j1").Copy Cells(Target.Row, 4)
            Range("E" & myRow).Value = Date
            Range("F" & myRow).Value = "Termed"
            Application.EnableEvents = False
            If IsEmpty(ws.Range("A" & myRow)) Then Sheet3.Range("D" & myRow).Value = ""
            If IsEmpty(ws.Range("A" & myRow)) Then Sheet3.Range("E" & myRow).Value = ""
            If IsEmpty(ws.Range("A" & myRow)) Then Sheet3.Range("F" & myRow).Value = ""
            Application.EnableEvents = True
        Next cel
    End If
    
    End Sub

  9. #9
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: VBA to delete rows based on user input

    I'm not able to replicate your file to solve this issue … Is the above worksheet event code placed in the same sheet where the DeleteEmployees macro is working ? What is the name of that sheet. Also, is Sheet3 a third sheet ? Please provide a small sample file to see the issue with the exact sheet names as per your file just with dummy data

  10. #10
    Registered User
    Join Date
    05-17-2020
    Location
    Philippines
    MS-Off Ver
    2016
    Posts
    35

    Re: VBA to delete rows based on user input

    Yes it is in the same sheet but the code is in the Object not in Module where the delete employee macro is. So in the event that an employee is deleted, the other code to watch for worksheet changes also kicks in and that's when the problem starts and the macro does not seem to stop running. I am not sure if its because of the Application.EnableEvents = True statement.

  11. #11
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: VBA to delete rows based on user input

    Try to put the red line in your worksheet event code & see if that solves the problem

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Dim cel As Range
    Dim myRow As Long
    
    If target.columns.count>1 then exit sub
    Set ws = ThisWorkbook.Sheets("Termed")
    
    If Not Intersect(Target, Range("A4:A13")) Is Nothing Then ' watch all the cells in this range
        For Each cel In Target ' do the next steps for each cell that was changed
            myRow = cel.Row
            Range("j1").Copy Cells(Target.Row, 4)
            Range("E" & myRow).Value = Date
            Range("F" & myRow).Value = "Termed"
            Application.EnableEvents = False
            If IsEmpty(ws.Range("A" & myRow)) Then Sheet3.Range("D" & myRow).Value = ""
            If IsEmpty(ws.Range("A" & myRow)) Then Sheet3.Range("E" & myRow).Value = ""
            If IsEmpty(ws.Range("A" & myRow)) Then Sheet3.Range("F" & myRow).Value = ""
            Application.EnableEvents = True
        Next cel
    End If
    
    End Sub

  12. #12
    Registered User
    Join Date
    05-17-2020
    Location
    Philippines
    MS-Off Ver
    2016
    Posts
    35

    Re: VBA to delete rows based on user input

    Thank you for the quick response. I'll try that and do some tests.

  13. #13
    Registered User
    Join Date
    05-17-2020
    Location
    Philippines
    MS-Off Ver
    2016
    Posts
    35

    Re: VBA to delete rows based on user input

    From the code above, could you help me edit this statement Range("j1").Copy Cells(Target.Row, 4) so that it does not copy the formatting, only the value?

  14. #14
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,713

    Re: VBA to delete rows based on user input

    You don't need the copy, you can assign the value directly like below

    Cells(Target.Row, 4) = Range("j1").value

  15. #15
    Registered User
    Join Date
    05-17-2020
    Location
    Philippines
    MS-Off Ver
    2016
    Posts
    35

    Re: VBA to delete rows based on user input

    You d' man! This is awesome! Thank you so much, sir!

+ 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] Delete specific number of rows for each column with user input
    By Sriramroxx in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-22-2019, 11:28 AM
  2. [SOLVED] delete last character based on user input
    By k1dr0ck in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-28-2018, 10:44 PM
  3. Replies: 15
    Last Post: 07-11-2017, 10:55 AM
  4. [SOLVED] VBA to delete columns based on user input
    By Muktar888 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-09-2017, 09:00 AM
  5. Macro to Delete Row based upon User Input Box value
    By Mike7591 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 09-18-2013, 10:02 AM
  6. Automatically Add/Delete rows based on user input but check current table row count
    By clemsoncooz in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-31-2011, 11:39 AM
  7. Delete rows based on user input
    By Militia in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-10-2009, 06:33 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