繁体   English   中英

Excel VBA .Find函数未找到日期

[英]Excel VBA .Find Function not finding a date

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

由于我不明白的原因,.Find方法在我的工作表中找不到任何日期。

日期格式为03/10/2017 17:05:00

但是,在关闭宏编辑器(使用了逐步调试功能)后,我可以按Ctrl + F并按Return键按预期循环浏览日期,宏甚至会填满窗口!

因此,我必须丢失一些东西,但是不会抛出任何错误。

c仍未定义。

count =一个整数,表示要在日期中循环的月份

c =查找到搜索字符串的范围(或在这种情况下应为)

d = kill开关,当循环最终导致脚本查找已经复制到左侧的第一个日期d时,应变为1并结束循环。 由于此错误,未经测试。

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

您的电子表格可能包含实际日期,而不仅仅是看起来像日期的文本,因此将字符串转换为日期:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM