macro to delete table row/entry

rjmdc

Active Member
Joined
Apr 29, 2020
Messages
261
Office Version
365
Platform
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
 

Some videos you may like

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
9,169
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
 

rjmdc

Active Member
Joined
Apr 29, 2020
Messages
261
Office Version
365
Platform
Windows
how
and when can i choose the entry i want to delete?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,594
Office Version
365
Platform
Windows
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
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
9,169
While the message box is visible, click on any cell in column A and then click the "OK" button.
 

rjmdc

Active Member
Joined
Apr 29, 2020
Messages
261
Office Version
365
Platform
Windows
hi
select entry cant be with an input box
i need the user to choose a row and begin in column A only
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
9,169
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,594
Office Version
365
Platform
Windows
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,242
Messages
5,485,607
Members
407,504
Latest member
inexperiencedOne

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top