簡體   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