VBA Delete Row From Table Based On Cell Selection

t0ny84

Board Regular
Joined
Jul 6, 2020
Messages
205
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
  2. Mobile
  3. Web
Hi All,

I have a a workbook with two tabs - Main Sheet and Schedule Tab, the events listed in a table on Schedule Tab (example below) show on the Main Sheet within the following cells based on the date selected. Whilst this works a treat I am trying to figure out how to let users delete events from the Schedule Tab. My idea was that if they select the Event Title Cell on the Main Sheet and click a delete button a macro would search the table on Schedule Tab for values matching the Event Title and corresponding date and then delete that row from the table. Does anyone know a way of doing this?

Schedule Tab
1598910692454.png


Main Sheet
1598910747424.png
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Well I have gotten to below but the ActiveCell.Offset seems to be not working!

VBA Code:
Sub DeleteEvent()
Dim lo As ListObject
  Set lo = Sheet5.ListObjects(1)
  lo.Parent.Activate   
  lo.AutoFilter.ShowAllData
  lo.Range.AutoFilter Field:=2, Criteria1:=ActiveCell.Value
  lo.Range.AutoFilter Field:=1, Criteria2:=ActiveCell.Offset(0,1).Value
  Application.DisplayAlerts = False
  lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
  Application.DisplayAlerts = True
  lo.AutoFilter.ShowAllData
End Sub
 
Upvote 0
Well I am nearly finished with this code I now just need to make it only delete the cells within the table and not the whole row!!!

VBA Code:
Sub DelCalEvent()

Dim testRange As Range 
Dim myRange As Range 
Dim lo As ListObject

' Check to make sure that user only selects cell in certain range.
Set testRange = Range("K6:K39") 'Set the range that macro will run
Set myRange = Selection 'the selection of user
Set lo = Sheet2.ListObjects(1) ' sheet name with table

 lo.AutoFilter.ShowAllData ' removes any filter

If Intersect(testRange, myRange) Is Nothing Then 'Check if the selection is inside the range.

'Selection is NOT inside the range.
   MsgBox "Selection is outside the test range, please ensure you are in the Event List."
    Else
'Selection IS inside the range.
 With lo.Range
.AutoFilter field:=4, Criteria1:="*" & Sheet2.Range("H2") & "*" & ActiveCell.Value
End With

If lo.Range.SpecialCells(xlCellTypeVisible).Count < 1 Then 'check if Autofilter has any results.
'if no results
MsgBox "No Data To Filter"
lo.AutoFilter.ShowAllData
Else
'if results
  Application.DisplayAlerts = False
' Delete Row
lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
Application.DisplayAlerts = True

lo.AutoFilter.ShowAllData 'Clear Filter
End If
End If
End Sub
 
Upvote 0
I found that by using the following code it meant that the additional data in the sheet didn't get deleted.

Code:
' clear cells found
lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Clear
' remove filter
lo.AutoFilter.ShowAllData
' delete blank cells
lo.DataBodyRange.SpecialCells(xlCellTypeBlanks).Delete

instead of below

Code:
lo.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
'Clear Filter
lo.AutoFilter.ShowAllData
 
Upvote 0
Cross posted Help Only Deleting Table Rows

While we do allow Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
Cross posted Help Only Deleting Table Rows

While we do allow Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules). This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
My bad, I thought I had included the link with this one and not just on the other thread...
 
Upvote 0

Forum statistics

Threads
1,215,688
Messages
6,126,210
Members
449,299
Latest member
KatieTrev

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