Excel VBA - 返回一列中找不到的值列表

Excel VBA - 返回一列中找不到的值列表

我想让 Excel VBA 在消息框提示中返回列中的所有项目A工作表的主表但不在列中A工作表的发货单

在弄清楚这一点上遇到了很多麻烦,以下是我目前得到的结果:

Private Sub CommandButton5_Click()

    Dim DeliveryName As Range
    Dim MasterName As Range
    Dim MasterSheet As Worksheet
    Dim DeliverySheet As Worksheet
    Dim valueToFind

    Set MasterSheet = Sheets("Delivery Master List Drop")
    Set DeliveryName = Sheets("For Delivery").Range(Sheets("For Delivery").Cells("A:A"))
    Set MasterName = Sheets("Delivery Master List Drop").Range(Sheets("Delivery Master List Drop").Cells("A:A"))

    For i = 3 To 3000
        valueToFind = DeliveryName("i,1")

        For Each MasterName In MasterSheet
            If Not MasterName.Cells = valueToFind Then
                MsgBox "The following name is not found in the Delivery Master List" & DeliveryName(i, 1).Value, vbExclamation
            End If
        Next MasterName
    Next i
End Sub

我希望消息框返回列表中所有未找到的项目(这些项目也不是 ("") 的值),该列表在宏完成后显示。现在我只是试图让它返回一个值。

答案1

Sub SearchForDeliveryItems()

    Dim wksMaster As Worksheet, wksSearch As Worksheet
    Dim rngMaster As Range, rngSearch As Range

    Set wksMaster = Sheets("Delivery Master List Drop")
    Set wksSearch = Sheets("For Delivery")

    With wksMaster
        Set rngMaster = .Range("A1:A" & .Range("A1").SpecialCells(xlCellTypeLastCell).Row)
    End With

    With wksSearch
        Set rngSearch = .Range("A1:A" & .Range("A1").SpecialCells(xlCellTypeLastCell).Row)
    End With

    With rngMaster
        For Each cll In rngSearch
            Set c = .Find(cll.Value2, LookIn:=xlValues)
            If c Is Nothing Then
                MsgBox cll.Value2 & " not found in the Delivery Master List."
            End If
        Next
    End With

End Sub

相关内容