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
 
thank you
you are correct, although it can be chosen it cannot be executed to delete
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
That's right.
There is nothing wrong with the code that Mumps' supplied, except you will need to change the Exit Sub lines if you want to reprotect the sheet.
 
Upvote 0
at the end before exit sub i added wsProtect
 
Upvote 0
If you just have that at the end your sheet can be left unprotected, when you get the message asking if you want to delete the entry select No & see what happens.
 
Upvote 0
so what would be a solution? can i add a protect if msgbox = no? and how would i do that?
 
Upvote 0
Easiest option is to call the protect macro just before any line that says "Exit Sub"
 
Upvote 0
thanks
nearly there
what do i do to return to sub instead of exit if choice was not in column a - how do i go back to choose what you want to delete?
VBA Code:
Sub DeletePrintCheck()
    Call WSUnProtect(Worksheets("Check Queue"))
    Dim rng As Range
    Set rng = Application.InputBox(prompt:="Select entry you want to delete.", Type:=8)
    If rng.Column <> 1 Or rng.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
        Call WSProtect(Worksheets("Check Queue"))
        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 = rng.Rows(1).Row
    slr = sr - rng.ListObject.Range.Row  'The starting List Row
    rng.ListObject.ListRows(slr).Delete
        Call WSProtect(Worksheets("Check Queue"))
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,218
Members
448,554
Latest member
Gleisner2

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