Find until Found not working

Emmett52

New Member
Joined
May 22, 2015
Messages
18
Hi,

First time poster so apologies if code is not formatted / presented correctly (im quite new to VBA)
I'm trying to find (In Column K) a date found in dataset, starting 5 days ago from today, then 6 days, then 7 days and so on until a date is found, and then clear date from offset (-1,0)
However, below doesn't seem to come out of the loop
Any help is much appreciated!


Sub DeleteAgedData()


Dim DateToday As Date
Dim DateAged As String
Dim datecounter As Integer


datecounter = 5

DateToday = Date
DateAged = DateToday - datecounter

Dim oSht As Worksheet
Dim lastRow, i As Long
Dim strSearch As String
Dim aCell As Range


Set oSht = Sheets("Sheet 0")
lastRow = oSht.Range("K" & Rows.Count).End(xlUp).Row
strSearch = DateAged

Do While aCell Is Nothing
Set aCell = oSht.Range("K4:K" & lastRow).Find(What:=strSearch, LookIn:=xlValues, LookAt:=xlPart)
datecounter = datecounter + 1
Loop

aCell.Offset(-1, 0).Select
ActiveCell.EntireRow.ClearContents

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi Emmett52,

It looks like you are not doing anything with your loops. Yes, you increment "datecounter" by 1 on each loop, but "datecounter" does not change strSearch. So if strSeach initially returns "is nothing", your endlessly looping.

Follow your logic, what part of your loop changes the value for strSeach. I think you would need something like this:

Code:
Do While aCell Is Nothing
    Set aCell = oSht.Range("K4:K" & lastRow).Find(What:=strSearch, LookIn:=xlValues, LookAt:=xlPart)
    datecounter = datecounter + 1
strSearch = DateToday - datecounter
    Loop

HTH

igold
 
Upvote 0

Forum statistics

Threads
1,213,585
Messages
6,114,518
Members
448,575
Latest member
hycrow

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