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
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try:
VBA Code:
Sub DeleteRow()
    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
        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
how
and when can i choose the entry i want to delete?
 
Upvote 0
How about
VBA Code:
Sub DeleteRow()
   Dim Rng As Range
   
   On Error Resume Next
   Set Rng = Application.InputBox("please select a cell in col A", , "A1", , , , , 8)
   On Error GoTo 0
   
   If Rng Is Nothing Then
      MsgBox "nothing selected"
      Exit Sub
   End If
   If Rng.CountLarge > 1 Then
       MsgBox "You must only select a single cell"
       Exit Sub
   End If
    
    If MsgBox("Are you sure you want to delete: " & Rng.Value & "?", vbYesNo + vbExclamation, "Confirm Delete") = vbNo Then
        Exit Sub
    End If
    
    Call WSUnProtect(Worksheets("Check Queue"))
    
   With Worksheets("Check Queue").ListObjects("tblCheckQueue")
      Set Rng = Intersect(.DataBodyRange, Rng.EntireRow)
      If Not Rng Is Nothing Then
         Rng.Delete
      Else
         MsgBox "You need to select a cell within the table"
      End If
   End With
    Call WSProtect(Worksheets("Check Queue"))
    
End Sub
 
Upvote 0
While the message box is visible, click on any cell in column A and then click the "OK" button.
 
Upvote 0
hi
select entry cant be with an input box
i need the user to choose a row and begin in column A only
 
Upvote 0
You don't enter any data into the input box. You simply click on any cell in column A and then click the "OK" button.
 
Upvote 0
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

Forum statistics

Threads
1,213,554
Messages
6,114,280
Members
448,562
Latest member
Flashbond

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