Looping through cells and deleting the entire row if the cell has a range of dates

excelnewb2020

New Member
Joined
Jan 13, 2021
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
I am having an issue writing a script that will loop through each cell and delete each row that contains a cell with a range of dates. For example, the below table should keep all of the rows that have a single date (i.e. 2021-01-12) while deleting all rows that contain a range (i.e. 2021-01-01 - 2021-01-02).

The script I have written so far does not work:

Sub test ()
Dim lastRow2 as Long
lastRow2 = Range("A" & Rows.Count).End(xlUp).Row
For Each c In Range("A2", "A" & lastRow2)
If Len(c) > 10 Then c.EntireRow.Delete
Next c
End sub

2021-01-12OtherData
2021-01-12OtherData
2021-01-12OtherData
2021-01-12OtherData
2021-01-12OtherData
2021-01-12OtherData
2021-01-12OtherData
2021-01-01 - 2021-01-02OtherData
2021-01-01 - 2021-01-02OtherData
2021-01-01 - 2021-01-02OtherData
2021-01-01 - 2021-01-02OtherData
2021-01-01 - 2021-01-02OtherData
2021-01-01 - 2021-01-02OtherData
2021-01-01 - 2021-01-02OtherData
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
When deleting you should loop from the bottom to the top.
VBA Code:
Sub test ()
Dim lastRow2 as Long

    lastRow2 = Range("A" & Rows.Count).End(xlUp).Row
    For idx = lastRow2 To 2 Step -1
        If Len(Range("A" & idx) > 10 Then Rows(idx).Delete
    Next idx

End sub
 
Upvote 0
Hi & welcome to MrExcel.
Another option is to delete all the rows in one go, like
VBA Code:
Sub test()
   Dim lastRow2 As Long
   Dim c As Range, Rng As Range

   lastRow2 = Range("A" & Rows.Count).End(xlUp).Row
   For Each c In Range("A2", "A" & lastRow2)
      If Len(c) > 10 Then
         If Rng Is Nothing Then Set Rng = c Else Set Rng = Union(Rng, c)
      End If
   Next c
   If Not Rng Is Nothing Then Rng.EntireRow.Delete
End Sub
 
Upvote 0
Thanks it works! Although the script takes quite awhile to execute (might be something unrelated though).
 
Upvote 0
Which code are you referring to?
 
Upvote 0
Hi & welcome to MrExcel.
Another option is to delete all the rows in one go, like
VBA Code:
Sub test()
   Dim lastRow2 As Long
   Dim c As Range, Rng As Range

   lastRow2 = Range("A" & Rows.Count).End(xlUp).Row
   For Each c In Range("A2", "A" & lastRow2)
      If Len(c) > 10 Then
         If Rng Is Nothing Then Set Rng = c Else Set Rng = Union(Rng, c)
      End If
   Next c
   If Not Rng Is Nothing Then Rng.EntireRow.Delete
End Sub
This one is amazing; much faster! Thank you so much for the help folks, I really appreciate it.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,166
Members
448,870
Latest member
max_pedreira

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