macro to delete table row/entry

rjmdc

Active Member
Joined
Apr 29, 2020
Messages
428
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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,409
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
428
Office Version
  1. 365
Platform
  1. Windows
how
and when can i choose the entry i want to delete?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
58,345
Office Version
  1. 365
Platform
  1. 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
10,409

ADVERTISEMENT

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
428
Office Version
  1. 365
Platform
  1. 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
10,409

ADVERTISEMENT

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
58,345
Office Version
  1. 365
Platform
  1. 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,133,146
Messages
5,657,096
Members
418,355
Latest member
michaelirl

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
Top