找不到任何东西

时间:2015-12-15 13:14:35

标签: excel vba excel-vba excel-2010

我无法在同一列内找到条目。为什么?我的代码出了什么问题?我得到类型不兼容的错误,因为它找到了第一个条目的Nothing。

Dim Namensspalte As Range, firstEntry As Range, secondEntry As Range
Set Namensspalte = iMAw.Range("E2:E1700")
With Namensspalte
For Each c In Namensspalte
    Set firstEntry = .Find(c, xlValues, xlWhole)
    If Not firstEntry Is Nothing Then
        firstEntry.Offset(0, 3).Value = firstEntry.Offset(0, -4).Value
    Else
        MsgBox "Error"
    End If
    Set secondEntry = .FindNext(c)
    If Not secondEntry Is Nothing Then
        secondEntry.Offset(0, 4).Value = secondEntry.Offset(0, -4).Value
    End If
Next
End With

1 个答案:

答案 0 :(得分:1)

这应该可以正常运作:

Sub test_HausUkrop()

Dim Namensspalte As Range, firstEntry As Range, secondEntry As Range, _
    c As Range, FirstAddress As String
Set Namensspalte = iMAw.Range("E2:E1700")
With Namensspalte
    For Each c In .Cells
        Set firstEntry = .Find(What:=c.Value2, _
                                After:=.Cells(1, 1), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByColumns, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False, _
                                SearchFormat:=False)
        If Not firstEntry Is Nothing Then
            FirstAddress = firstEntry.Address
            firstEntry.Offset(0, 3).Value = firstEntry.Offset(0, -4).Value

            Set secondEntry = .FindNext(firstEntry)
            'Look until you find again the first result
            If Not secondEntry Is Nothing Then
                If secondEntry.Address <> FirstAddress Then
                    secondEntry.Offset(0, 4).Value = secondEntry.Offset(0, -4).Value
                Else
                End If
            Else
            End If
        Else
            MsgBox "Error"
        End If
    Next c
End With

End Sub