VBA - Delete Row in Table based on TextBox (Userform) Value

sprigelz

Board Regular
Joined
Jan 7, 2016
Messages
98
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to write a VBA script that will find a specific row in a table and delete it. The script should look at the value in a userform, search the table and delete that specific row.

Userform - RemEmpValidate
TextBox - TextBox2
Table - "Employees" on Worksheet "Employee List"

Textbox2 contains a Last Name value which will match with Table - Employees[Last Name] (Column C)

Can anyone help?
 
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
I attempted to write something like the below, but this doesn't seem to do anything :(

This just seems to give me an Object Required error.
VBA Code:
Dim k As Integer
Dim tbl As Integer
Set tbl = Worksheets("Employee List").ListObjects("Employees").Range.Rows.Count

For k = tbl To 1 Step -1
If Cells(k, 12).Value = EmpID.Value Then
Cells(k, 1).EntireRow.Delete
End If
Next k
 
Upvote 0
If I have my code in a standard module, I would call my module using something like this:
VBA Code:
Private Sub CommandButton1_Click()
    Call DeleteEmployeeID(Me.TextBox2)
End Sub

And my deletion code would be something like this:
(where 12 is the column in the table)

VBA Code:
Sub DeleteEmployeeID(sEmp As String)

    Dim k As Long
    Dim tbl As ListObject
    Dim tblNoOfRows As Long
    
    Set tbl = Worksheets("Employee List").ListObjects("Employees")
    tblNoOfRows = tbl.DataBodyRange.Rows.Count
    
    For k = tblNoOfRows To 1 Step -1
        If tbl.DataBodyRange.Cells(k, 12).Value = sEmp Then
            tbl.ListRows(k).Delete
        End If
    Next k

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,213,487
Messages
6,113,943
Members
448,534
Latest member
benefuexx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top