Macro to Delete All Data in Table(sheet Daily Cases) except Date in Cell B2 on sheet Import Data

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have a table on Sheet "Daily Cases"

I have a the last date used on sheet Summary in cell B2 named "Date"

At the beginning of a new month I want to delete all the data in the Table except the Date in cel B2 named Date which I have set up as the criteria in my Code

The dates in the table are in Col from row 2 onwards

Code:
 Sub Delete_TableRows()
Dim ws As Worksheet, LR As Long
With Sheets("Daily Cases")
LR = .Cells(.Rows.Count, "A").end(xlup).row
  Set ws = ThisWorkbook.Worksheets("Daily Cases")
  ws.Activate
 
  On Error Resume Next
    ws.ShowAllData
  On Error GoTo 0
  
  ws.Range("A2:C" & LR).AutoFilter Field:=2, Criteria1:="<>date"
  
  Application.DisplayAlerts = False
    ws.Range("B2:C" & LR).SpecialCells(xlCellTypeVisible).Delete
  Application.DisplayAlerts = True
  
  
  On Error Resume Next
    ws.ShowAllData
  On Error GoTo 0
End With

End Sub


When running the macro, Col B & C are being Deleted

it would be appreciated if someone could amend my code
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
If this line
VBA Code:
 ws.Range("A2:C" & LR).AutoFilter Field:=2, Criteria1:="<>date"
hides multiple lines, they won't be deleted !
Would changing this line
VBA Code:
ws.Range("B2:C" & LR).SpecialCells(xlCellTypeVisible).Delete
to
VBA Code:
ws.Range("B2:C" & LR).clear
do the trick ??
 
Upvote 0
Thanks Michael, but Code clears columns B & C

I wants all the rows to be deleted except the dates that are the same date as cell B2 on sheet Summary

The date currently is 30/5/2021 (dd/mm/yyyy)

See Sample data of my table below


Detail of Covid19AttributeValue
Coronavirus Cases30/04/20211581210
Coronavirus Cases01/05/20211582842
Coronavirus Cases02/05/20211584064
Coronavirus Cases03/05/20211584961
Coronavirus Cases04/05/20211586148
Coronavirus Cases05/05/20211588221
Coronavirus Cases06/05/20211590370
Coronavirus Cases07/05/20211592326
Coronavirus Cases08/05/20211594817
Coronavirus Cases09/05/20211596595
Coronavirus Cases10/05/20211597724
Coronavirus Cases11/05/20211599272
Coronavirus Cases12/05/20211602031
Coronavirus Cases13/05/20211605252
Coronavirus Cases14/05/20211605252
Coronavirus Cases15/05/20211611143
Coronavirus Cases16/05/20211613728
Coronavirus Cases17/05/20211615485
Coronavirus Cases18/05/20211617840
Coronavirus Cases19/05/20211621362
Coronavirus Cases20/05/20211625003
Coronavirus Cases21/05/20211628335
Coronavirus Cases22/05/20211632571
Coronavirus Cases23/05/20211635465
Coronavirus Cases24/05/20211637848
Coronavirus Cases25/05/20211640932
Coronavirus Cases26/05/20211645555
Coronavirus Cases27/05/20211649977
Coronavirus Cases28/05/20211654551
Coronavirus Cases30/05/20211659070
Deaths01/05/202154406
Deaths02/05/202154417
Deaths03/05/202154452
Deaths04/05/202154511
Deaths05/05/202154557
Deaths06/05/202154620
Deaths07/05/202154687
Deaths08/05/202154724
Deaths09/05/202154735
Deaths10/05/202154825
Deaths11/05/202154896
Deaths12/05/202154968
Deaths13/05/202155012
Deaths14/05/202155124
Deaths15/05/202155183
Deaths16/05/202155210
Deaths17/05/202155260
Deaths18/05/202155340
Deaths19/05/202155507
Deaths20/05/202155568
Deaths21/05/202155719
Deaths22/05/202155772
Deaths23/05/202155802
Deaths24/05/202155874
Deaths25/05/202155976
Deaths26/05/202156077
Deaths27/05/202156170
Deaths28/05/202156293
Deaths30/04/202154350
Recovered01/05/20211506732
Recovered02/05/20211507778
Recovered03/05/20211508558
Recovered04/05/20211509656
Recovered05/05/20211510385
Recovered06/05/20211511905
Recovered07/05/20211513202
Recovered08/05/20211514088
Recovered09/05/20211516256
Recovered10/05/20211517350
Recovered11/05/20211519258
Recovered12/05/20211519734
Recovered13/05/20211519734
Recovered14/05/20211519734
Recovered15/05/20211519734
Recovered16/05/20211524352
Recovered17/05/20211526638
Recovered18/05/20211527968
Recovered19/05/20211528868
Recovered20/05/20211531993
Recovered21/05/20211534350
Recovered22/05/20211537430
Recovered23/05/20211539395
Recovered24/05/20211541250
Recovered25/05/20211543951
Recovered26/05/20211546583
Recovered27/05/20211548092
Recovered28/05/20211551520
Deaths30/05/202156363
Recovered30/04/20211505620
Recovered30/05/20211554184
 
Upvote 0
Hi Michael

Not sure if you saw my my table in post # 3 ? It would be appreciated if you will make the necessary changes
 
Upvote 0
Is this table also on Sheet "Summary"?
If not what is the sheet name
 
Upvote 0
Try this
VBA Code:
Sub MM1()
Dim lr As Long, r As Long
lr = Sheets("Daily Cases").Cells(Rows.Count, "A").End(xlUp).Row
With Sheets("Daily Cases")
For r = lr To 2 Step -1
If .Cells(r, 2) <> Sheets("Summary").Cells(2, 2) Then
.Rows(r).Delete
End If
Next r
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,720
Members
448,986
Latest member
andreguerra

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