在整个工作簿中查找date(string变暗)

我想search一个date的所有工作表,并find时用“是”取代date。

我有下面的代码:

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 

然而,代码运行,但它永远不会到达每个循环。 当我删除“ – ”,所以date将成为一个数字它find它。 但是,当破折号出现的代码没有find任何合适的值,但是这些都存在。

尝试这一点, for each ... next方法与比较.Text 42366 ,因为"28-12-2015" 42366 "28-12-2015"的单元格的值是42366 ),它运作良好,但一些用户反对这种方法的一些原因,至于我更less编码更好的阅读

 Sub Aftekenen() Dim Sh As Worksheet, Loc As Range, Datum As String Application.ScreenUpdating = 0 Datum = "28-12-2015" For Each Sh In ThisWorkbook.Worksheets For Each Loc In Sh.UsedRange If Loc.Text = Datum Then Loc.Value = "Yes" Next Loc Next Sh Application.ScreenUpdating = 1 End Sub 

更新

如果您更喜欢.find方法,那么为了searchdate,需要将Application.FindFormat.NumberFormat切换为带有date的单元格的格式,由于.text的date是18-05-2015 ,但.value42142

下面是你更新的变种

 Sub Aftekenen() Dim Sh As Worksheet Dim Loc As Range Dim Datum As String Application.FindFormat.NumberFormat = "dd-mm-yyyy" Datum = "28-12-2015" For Each Sh In ThisWorkbook.Worksheets With Sh.UsedRange Set Loc = .Cells.Find(Datum, , xlValues, , , , True, , True) 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