Delete Table Rows base on selected cells

gberg

Board Regular
Joined
Jul 16, 2014
Messages
180
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

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Is the ICO in a particular column? If so what is the name of that column?
 
Upvote 0
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
 
Upvote 0
Solution
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
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,636
Messages
6,120,668
Members
448,977
Latest member
moonlight6

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