Delete Table Rows base on selected cells

gberg

Board Regular
Joined
Jul 16, 2014
Messages
75
Office Version
  1. 365
Platform
  1. Windows
I'm trying to delete Table Rows based on which cell(s) are selected. I have a macro that will select the cells for the rows I want to delete (any row with a cell value of "ICO"). I can't figure out how to delete the Table Rows for the selected cells. This is what I have but the "Selection.EntireRow.Delete" apparently does not work in a Table

Sub ico()

'declare variables
Dim ws As Worksheet
Dim SelectCells As Range
Dim xcell As Object
Set ws = Worksheets("Job Log - Client")

Range("Setup").Select

'check each cell in the specific worksheet if the criteria is matching
On Error GoTo ErrorHandler
For Each xcell In ws.UsedRange.Cells
If xcell.Value = "ICO" Then
If SelectCells Is Nothing Then
Set SelectCells = Range(xcell.Address)
Else
Set SelectCells = Union(SelectCells, Range(xcell.Address))
End If
End If

Next

'select the cells with specified value
SelectCells.Select

Selection.EntireRow.Delete

ErrorHandler:

End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,608
Office Version
  1. 365
Platform
  1. Windows
Is the ICO in a particular column? If so what is the name of that column?
 

gberg

Board Regular
Joined
Jul 16, 2014
Messages
75
Office Version
  1. 365
Platform
  1. Windows
"Type" is the name of the column
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,608
Office Version
  1. 365
Platform
  1. Windows
Ok, how about
VBA Code:
Sub gberg()

'declare variables
Dim ws As Worksheet
Dim SelectCells As Range, xcell As Range
Dim Tbl As ListObject

Set ws = Worksheets("Job Log - Client")
Set Tbl = ws.ListObjects("setup")

For Each xcell In Tbl.ListColumns("Type").DataBodyRange
   If xcell.Value = "ICO" Then
      If SelectCells Is Nothing Then
         Set SelectCells = xcell
      Else
         Set SelectCells = Union(SelectCells, xcell)
      End If
   End If
Next xcell

Intersect(Tbl, SelectCells.EntireRow).Delete

End Sub

In future when posting code, please use code tags. How to Post Your VBA Code
 
Solution

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,705
Office Version
  1. 365
  2. 2010
Platform
  1. Windows

ADVERTISEMENT

This would be a generic code as I am not sure as what your table name is and what sheet it is on. This assumes "Table1" on "Sheet1"

VBA Code:
Sub LookupTableValue()

    Dim ws As Worksheet
    Dim tbl As ListObject
    Dim fc As Range
    Dim srch As String
    Dim r As Long

    Set ws = Worksheets("Sheet1")
    Set tbl = ws.ListObjects("Table1")
    srch = "ICO"
    
    For r = tbl.DataBodyRange.Rows.Count To 1 Step -1
        Set fc = tbl.DataBodyRange.Rows(r).Find(srch, LookAt:=xlWhole)
        If Not fc Is Nothing Then
            tbl.DataBodyRange.Rows(r).Delete
        End If
    Next

End Sub
 

gberg

Board Regular
Joined
Jul 16, 2014
Messages
75
Office Version
  1. 365
Platform
  1. Windows
Did the trick, thanks for your help!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
65,608
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

Forum statistics

Threads
1,147,560
Messages
5,741,825
Members
423,689
Latest member
Jords998

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