Excel VBA。find函数找不到date

我正在使用下面的VBA代码来select列中的每个数据的第一个实例,并将其复制到左侧的新列。

由于我不明白的原因,.Find方法没有find我的工作表中的任何date。

date格式为03/10/2017 17:05:00

但是,在closuresmacros编辑器(已经使用脚步进行debugging)后,我可以按Ctrl + F和Return按照预期的周期循环,macros甚至填充窗口!

所以我一定会错过一些东西,但没有错误被抛出。

c仍未定义。

count =一个整数,表示月份在date间循环的date

c =findsearchstring的范围(或者在这种情况下应该是)

d = kill开关,当循环最终导致脚本find已经被复制到左边的第一个dated应该变成1并结束循环。 由于这个错误未经testing。

cell =纯粹用于用#N / A填充新列中剩余的单元格

正如你可能会告诉我非常在这里业余爱好者,我欢迎和欣赏任何帮助或反馈。

Sub LabelMaker() ' ' LabelMaker Macro ' Dim count As Integer count = 2 Dim c As Range Dim s As String Dim d As Integer d = 0 Dim cell As Range Dim ws As Worksheet Set ws = ActiveSheet With ws Columns("C:C").Insert Shift:=xlToRight Range("C1") = "Labels" Do s = CStr(count) & "/10/2017" Set c = .Range("D:D").Find(s, LookIn:=xlValues, LookAt:=xlPart) If Not c Is Nothing Then If IsEmpty((c.Offset(rowOffset:=0, columnOffset:=-1))) Then c.Copy _ Destination:=c.Offset(rowOffset:=0, columnOffset:=-1) Else d = 1 End If End If count = count + 1 If count = 32 Then count = 1 End If Loop While d = 0 For Each cell In Range("C:C") If IsEmpty(cell) Then cell = "#N/A" End If Next End With ' End Sub 

您的电子表格可能包含实际date,而不仅仅是看起来像date的文本,所以将string转换为date:

 Set c = .Range("D:D").Find(CDate(s), LookIn:=xlValues, LookAt:=xlPart)