Forum Discussion
Trouble deleting rows in a table
It is possible to achieve what you are trying to do. The error you are encountering is likely due to the protection settings of the worksheet. Since you want to delete rows and sort the table using VBA, you need to temporarily unprotect the worksheet to perform these actions and then protect it again after making the changes.
Here is an updated version of your VBA code that should work correctly:
vba code (untested😞
Sub RemoveEmployeeAndSort()
Dim tbl As ListObject
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Set ws = ThisWorkbook.Sheets("Tables1") ' Replace with your sheet name
Set tbl = ws.ListObjects("EE_DB") ' Replace with your table name
' Unprotect the worksheet to make changes
ws.Unprotect
' Find the last row with data in the table
lastRow = tbl.ListRows.Count
' Loop through the table rows in reverse order
For i = lastRow To 2 Step -1
If tbl.ListRows(i).Range.Cells(1, 1).Value = "" Then
' Clear contents if it's a blank row
tbl.ListRows(i).Range.ClearContents
End If
Next i
' Sort the table using VBA
With tbl.Sort
.SortFields.Clear
.SortFields.Add Key:=tbl.ListColumns(1).Range, SortOn:=xlSortOnValues, Order:=xlAscending
.Header = xlYes
.Apply
End With
' Reprotect the worksheet without a password
ws.Protect UserInterfaceOnly:=True
End Sub
Make sure to replace "Tables1" with the actual name of your worksheet, and "EE_DB" with the name of your table. Also, ensure that the worksheet is protected with the "UserInterfaceOnly" parameter set to "True" to allow VBA to make changes while the worksheet is protected.
After running this code, the blank rows should be removed from the table, and the remaining rows should be sorted alphabetically based on column B, without affecting the formulas in column A.
The text, steps and Vba Code were created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
what it really comes down to I Microsoft has failed the US. This company is the reason why china caught but to the US ..just look at their customer service..the CEO should resign..