I want to search all worksheets for a date, and when found replace the date by "yes".
I have the code below:
Sub Aftekenen()
Dim Sh As Worksheet
Dim Loc As Range
Dim Datum As String
Datum = "28-12-2015"
For Each Sh In ThisWorkbook.Worksheets
With Sh.UsedRange
Set Loc = .Cells.Find(What:=Datum)
If Not Loc Is Nothing Then
Do Until Loc Is Nothing
Loc.Value = "Yes"
Set Loc = .FindNext(Loc)
Loop
End If
End With
Set Loc = Nothing
Next
End Sub
However the code runs, however it never reaches the For each loop. When I remove the "-" so the date will become a number it finds it. However when the dashes are present the code does not find any fitting value, however these are present.
try this,
for each ... next
method with comparing.Text
(not value, because of value of the cell for"28-12-2015"
is42366
), it works well but some users have an objection to this method by some reasons, as for me less coding better readingupdate
if you prefer
.find
method then for searching of the dates is required to switchApplication.FindFormat.NumberFormat
to format of the cells with dates, due to the date in.text
is18-05-2015
, but.value
is42142
below is your updated variant