为什么我的VBA。找不到任何东西?

时间:2017-08-23 20:01:48

标签: excel vba excel-vba

我认为这很简单.Find,但它不起作用。

lngLastRow = wsFound.Range("D" & Rows.count).End(xlUp).Row
Set SearchRange = wsFound.Range("D1:D" & lngLastRow)
For Each a In wsFound.Range(wsFound.Range("D2"), wsFound.Range("D" & Rows.count).End(xlUp))
    With SearchRange
        Set c = .Find("01/03/1950", LookIn:=xlValues) 'a.Value, LookIn:=xlValues)
        If Not c Is Nothing Then
            Firstfind = a.Address
            Do
                wsFound.Range("A" & a.Row & ":U" & a.Row).Copy
                LastRow = ActiveWorkbook.Sheets("Duplicates").Range("A" & Rows.count).End(xlUp).Row + 1
                ActiveWorkbook.Sheets("Duplicates").Range("A" & LastRow).PasteSpecial
                Set c = .FindNext(c)
                If c Is Nothing Then
                    GoTo DoneFinding
                End If
            Loop While a.Address <> Firstfind
        End If

DoneFinding:         结束     接下来

我非常确定我已经正确使用MSDN中的信息来实现这一目标。

但它没有找到任何东西!

我的数据如下:

+--------------+---------+-----------+---------------+--------------+
|     A        |    B    |     C     |       D       |       E      |
+--------------+---------+-----------+---------------+--------------+
| Staff Number | Surname | Forenames | Date of Birth |  Address 1   |
+--------------+---------+-----------+---------------+--------------+
|   1000064036 | Farrell | Margaret  | 01/03/1950    | 11 The Close |
|   1000064036 | Farrell | Margaret  | 01/03/1950    | 11 The Close |
|   1000064036 | Farrell | Margaret  | 01/03/1950    | 11 The Close |
+--------------+---------+-----------+---------------+--------------+

所以我知道应该找到DOB D列中有3个重复项。

1 个答案:

答案 0 :(得分:1)

根据David Zemens的评论,我最终通过更多的搜索来弄清楚我的搜索更具体。

这是有效的:

For Each a In wsFound.Range(wsFound.Range("D2"), wsFound.Range("D" & Rows.count).End(xlUp))
    With SearchRange
        Set c = SearchRange.Find(a.Value, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext)
        If Not c Is Nothing Then
            Firstfind = a.Address
            Do
                wsFound.Range("A" & a.Row & ":U" & a.Row).Copy
                LastRow = ActiveWorkbook.Sheets("Duplicates").Range("A" & Rows.count).End(xlUp).Row + 1
                ActiveWorkbook.Sheets("Duplicates").Range("A" & LastRow).PasteSpecial
                Set c = .FindNext(c)
                If c Is Nothing Then
                    GoTo DoneFinding
                End If
            Loop While a.Address <> Firstfind
        End If
DoneFinding:
    End With
Next a

我必须将LookIn值从xlValues更改为xlformulas,现在即使不使用CDate,它也会查找我正在寻找的日期。

我在另一个问题的评论中发现了这一点。 Find date value in a column VBA,评论由Eric K

撰写