macro to delete table row/entry

rjmdc

Well-known Member
Joined
Apr 29, 2020
Messages
672
Office Version
  1. 365
Platform
  1. Windows
hi
this is the code i am working with. i am missing something at the beginning because it doesnt allow for choice of the entry to delete
VBA Code:
Sub DeleteRow()

 Call WSUnProtect(Worksheets("Check Queue"))
 
 MsgBox "Choose entry you want to delete."

    If Selection.Column <> 1 Or Selection.Cells.Count <> 1 Then
        MsgBox "You must be in Column A to perform the delete function."
        Exit Sub
    End If
    
    If MsgBox("Are you sure you want to delete: " & Selection.Value & "?", vbYesNo + vbExclamation, "Confirm Delete") = vbNo Then
        Exit Sub
    End If
    
    Call WSUnProtect(Worksheets("Check Queue"))
    
    Dim tbl As ListObject, LastRow As Range
    Dim col As Long
    Set tbl = Worksheets("Check Queue").ListObjects("tblCheckQueue")
    Dim sr As Long 'Actual Row
    Dim slr As Long 'Start List Row
    sr = Selection.Rows(1).Row
    slr = sr - Selection.ListObject.Range.Row  'The starting List Row
    
    Selection.ListObject.ListRows(slr).Delete

    Call WSProtect(Worksheets("Check Queue"))
    
End Sub
 
Word of warning, with this at the top of the code
VBA Code:
Call WSUnProtect(Worksheets("Check Queue"))
If the first message box comes up, or they select No on the 2nd one, your sheet will be unprotected.
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
hi fluff
i am a bit confused
i tried your code
if the unprotect isnt on the top then user cannot choose a cell as the entire sheet is protected
however it isnt easy to choose a cell in column a when executing your code, it first asks to delete wherever the cursor happens to be
 
Upvote 0
If the entire sheet is locked down, then you cannot use exit sub in the code.
Try selecting No on the message box & see what happens. ;)
 
Upvote 0
hi
your macro allows a choice outside of column A
 
Upvote 0
Does that matter as long as the the selection is inside the table?
 
Upvote 0
for sure as it is allowing deletion of the column titles
 
Upvote 0
It will not allow you delete the header row.
 
Upvote 0
it did
what did i do wrong
i copied your code exactly
 
Upvote 0
It cannot delete the header row, because it's not part of the Data body range.
With this data
PostcodeIn Use?LatitudeLongitudeEastingNorthingGridRef
S36 1GJYes53.472455-1.601497426549397376SK265973
OL4 4BAYes53.546558-2.059182396176405548SD961055
DE4 2BUYes53.145179-1.664349422548360945SK225609
BD6 3FHYes53.767997-1.803484413051430201SE130302
BD20 6QXYesAED-1.952001403253442777SE032427
HD3 3UHYes53.659087-1.853093409806418076SE098180
HD7 3EBNoAED-1.826124411609409593SE116095
DH8 5EJYes54.853973-1.839415410408551031NZ104510
DL8 5JZYesAED-1.850724409807492011SE098920
S10 3PHYesAED-1.539468430738386290SK307862
DL8 3SHYesAED-2.200844387027489389SD870893
HD3 3GSYes53.659793-1.842574410501418156SE105181


If I selected any cell in A1:G1, this message box would be displayed
VBA Code:
MsgBox "You need to select a cell within the table"
 
Upvote 0

Forum statistics

Threads
1,214,909
Messages
6,122,189
Members
449,072
Latest member
DW Draft

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