vba to delete a selected row in a table

dpaton05

Well-known Member
Joined
Aug 14, 2018
Messages
2,392
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  • I have some code that is meant to allow a row or a cell in that row to be selected and the row will be deleted upon a button click. If it is the first row in the table, I just need it cleared. I am sure the code used to work, but for some reason, it has stopped working.
  • What it does now is it clears the first row, even if you have another row selected.
  • I also don't want a run time error to appear if no cells are found.

Here is the code I have. Can someone help me please?


Code:
Sub DelSelectCostingRow()
    ActiveSheet.Unprotect Password:="npssadmin"
        Dim rng As Range
        Dim tbl As ListObject
            Set tbl = ActiveSheet.ListObjects("tblCosting")
        On Error Resume Next
        With Selection.Cells
            Set rng = Intersect(.EntireRow, ActiveCell.ListObject.DataBodyRange)
                On Error GoTo 0
                If rng Is Nothing Then
                    MsgBox "Please select a cell within a row that you want to delete.", vbCritical
                Else
                    ActiveCell.EntireRow.Select
                    If ActiveSheet.ListObjects("tblCosting").ListRows(1).Range.Select Then
                        tbl.DataBodyRange.Rows(1).SpecialCells(xlCellTypeConstants).ClearContents
                    Else
                        rng.Delete xlShiftUp
                    End If
                End If
        End With
    Application.EnableEvents = True
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi DAve didn't a previous version of this code work fine ??
Version 12.7 of the quoting tool worked OK !
 
Upvote 0
Maybe this version, if it is the Costing Tool SHeet

Code:
Private Sub cmdDelSelectRow_Click()
    ActiveSheet.Unprotect Password:="npssadmin"
        Dim rng As Range
        On Error Resume Next
        With Selection.Cells(1)
            Set rng = Intersect(.EntireRow, ActiveCell.ListObject.DataBodyRange)
            On Error GoTo 0
            If rng Is Nothing Then
                MsgBox "Please select a cell within a row that you want to delete.", vbCritical
            Else
                If ActiveCell.Row = 5 Then ActiveCell.Row.ClearContents
                rng.Delete xlShiftUp
            End If
        End With
    Application.EnableEvents = True
    ActiveSheet.Protect Password:="npssadmin"
End Sub
 
Upvote 0
Sorry Dave....the code in the last post won't work.
Try this one

Code:
Private Sub cmdDelSelectRow_Click()
    ActiveSheet.Unprotect Password:="npssadmin"
        Dim rng As Range
        
        On Error Resume Next
        With Selection.Cells(1)
            Set rng = Intersect(.EntireRow, ActiveCell.ListObject.DataBodyRange)
            On Error GoTo 0
            If rng Is Nothing Then
                MsgBox "Please select a cell within a row that you want to delete.", vbCritical
            Else
                If ActiveCell.Row = 5 Then
                Rows(5).ClearContents
                Else
                rng.Delete xlShiftUp
                End If
            End If
        End With
    'ListObjects("NPSS_quote").ListColumns("10%Increase").DataBodyRange.Value = "1"
    Application.EnableEvents = True
    'ActiveSheet.Protect Password:="npssadmin"
End Sub
 
Last edited:
Upvote 0
Maybe this version, if it is the Costing Tool SHeet

Code:
Private Sub cmdDelSelectRow_Click()
    ActiveSheet.Unprotect Password:="npssadmin"
        Dim rng As Range
        On Error Resume Next
        With Selection.Cells(1)
            Set rng = Intersect(.EntireRow, ActiveCell.ListObject.DataBodyRange)
            On Error GoTo 0
            If rng Is Nothing Then
                MsgBox "Please select a cell within a row that you want to delete.", vbCritical
            Else
                If ActiveCell.Row = 5 Then ActiveCell.Row.ClearContents
                rng.Delete xlShiftUp
            End If
        End With
    Application.EnableEvents = True
    ActiveSheet.Protect Password:="npssadmin"
End Sub

I get an error with this code. It highlights the second .row in this line
If ActiveCell.Row = 5 Then ActiveCell.Row.ClearContents
and says invalid qualifier.
 
Upvote 0
See post #4 Dave.
So if it is the only row in the table, you don't want to clear the row ??
You only want to clear a range of cells...is that correct.

AND
I thought you had code to insert the formulas when rows were copied to the costing tool Sheet ??
 
Upvote 0
I tried using the code I already have that inserts a formula but it has a syntax error. The code was on the sheet NPSS_quote_sheet but I am trying to get it to work on Costing tool and I get a syntax error with trying to insert this formula
Code:
        With ThisWorkbook.Worksheets("Costing_tool")
            .Range("I5").Formula = "=IF(E5="Activities",0,[Price
ex. GST]*0.1)"

        End With
 
Upvote 0
I have this formula that works in the spreadsheet so I was just trying to use vba to insert it.
Code:
=IF(E5="Activities",0,[Price 
ex. GST]*0.1)
 
Upvote 0
you could use

Code:
Worksheets("Costing_tool").Range("I5").Formula = "=IF(E5=""Activities"",0,H5*0.1)"

OR simply clear the required cells

Code:
Private Sub cmdDelSelectRow_Click()
    ActiveSheet.Unprotect Password:="npssadmin"
        Dim rng As Range
        
        On Error Resume Next
        With Selection.Cells(1)
            Set rng = Intersect(.EntireRow, ActiveCell.ListObject.DataBodyRange)
            On Error GoTo 0
            If rng Is Nothing Then
                MsgBox "Please select a cell within a row that you want to delete.", vbCritical
            Else
                If ActiveCell.Row = 5 Then
                Range("A5:H5").ClearContents
                Else
                rng.Delete xlShiftUp
                End If
            End If
        End With
    'ListObjects("NPSS_quote").ListColumns("10%Increase").DataBodyRange.Value = "1"
    Application.EnableEvents = True
    'ActiveSheet.Protect Password:="npssadmin"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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