Excel 如何查看哪些列A条目未出现在列B中?

Excel 如何查看哪些列A条目未出现在列B中?,excel,vba,match,Excel,Vba,Match,我试图查看哪些A列条目不在B列中,然后在C列中打印这些条目,并在D列中打印相应的B条目。 这段代码一直给我“运行时错误'13':类型不匹配” 你可以试试: 回答每个循环: Option Explicit Sub test() Dim LastRowC As Long Dim rngA As Range, rngB As Range, cell With ThisWorkbook.Worksheets("Sheet1") Set rngA = .R

我试图查看哪些A列条目不在B列中,然后在C列中打印这些条目,并在D列中打印相应的B条目。 这段代码一直给我“运行时错误'13':类型不匹配”

你可以试试:

回答每个循环:

Option Explicit

Sub test()

    Dim LastRowC As Long
    Dim rngA As Range, rngB As Range, cell

    With ThisWorkbook.Worksheets("Sheet1")

        Set rngA = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        Set rngB = .Range("B2:B" & .Cells(.Rows.Count, "B").End(xlUp).Row)

        For Each cell In rngA

            If Application.WorksheetFunction.CountIf(rngB, cell) Then
            Else
                .Range("C" & .Cells(.Rows.Count, "C").End(xlUp).Row + 1).Value = cell.Value
            End If
        Next

    End With

End Sub
Option Explicit

Sub test()

    Dim LastRowC As Long
    Dim arrA As Variant, arrB As Variant, i As Long, y As Long
    Dim strA As String, strB As String
    Dim Appears As Boolean

    With ThisWorkbook.Worksheets("Sheet1")

        arrA = Application.Transpose(.Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row))
        arrB = Application.Transpose(.Range("B2:B" & .Cells(.Rows.Count, "B").End(xlUp).Row))

        For i = 1 To UBound(arrA)

            strA = arrA(i)

            Appears = False

            For y = 1 To UBound(arrB)

                strB = arrB(y)

                If strA = strB Then
                    Appears = True
                    Exit For
                Else
                    Appears = False
                End If
            Next y

            If Appears = False Then
                .Range("C" & .Cells(.Rows.Count, "C").End(xlUp).Row + 1).Value = arrA(i)
            End If

        Next i

    End With

End Sub
Sub tesqt()

    Dim dictA As Object, dictB As Object
    Dim i As Long
    Dim cell As Range
    Dim key As Variant

    Set dictA = CreateObject("Scripting.Dictionary")
    Set dictB = CreateObject("Scripting.Dictionary")

    With ThisWorkbook.Worksheets("Sheet1")

        For Each cell In .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
            If Not dictA.Exists(cell.Value) Then
                dictA.Add key:=cell.Value, item:=dictA.Count + 1
            End If
        Next

        For Each cell In .Range("B2:B" & .Cells(.Rows.Count, "B").End(xlUp).Row)
            If Not dictB.Exists(cell.Value) Then
                dictB.Add key:=cell.Value, item:=dictB.Count + 1
            End If
        Next

        For Each key In dictA.keys

            If Not dictB.Exists(key) Then
                 .Range("C" & .Cells(.Rows.Count, "C").End(xlUp).Row + 1).Value = key
            End If

        Next

    End With

End Sub
用数组回答:

Option Explicit

Sub test()

    Dim LastRowC As Long
    Dim rngA As Range, rngB As Range, cell

    With ThisWorkbook.Worksheets("Sheet1")

        Set rngA = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        Set rngB = .Range("B2:B" & .Cells(.Rows.Count, "B").End(xlUp).Row)

        For Each cell In rngA

            If Application.WorksheetFunction.CountIf(rngB, cell) Then
            Else
                .Range("C" & .Cells(.Rows.Count, "C").End(xlUp).Row + 1).Value = cell.Value
            End If
        Next

    End With

End Sub
Option Explicit

Sub test()

    Dim LastRowC As Long
    Dim arrA As Variant, arrB As Variant, i As Long, y As Long
    Dim strA As String, strB As String
    Dim Appears As Boolean

    With ThisWorkbook.Worksheets("Sheet1")

        arrA = Application.Transpose(.Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row))
        arrB = Application.Transpose(.Range("B2:B" & .Cells(.Rows.Count, "B").End(xlUp).Row))

        For i = 1 To UBound(arrA)

            strA = arrA(i)

            Appears = False

            For y = 1 To UBound(arrB)

                strB = arrB(y)

                If strA = strB Then
                    Appears = True
                    Exit For
                Else
                    Appears = False
                End If
            Next y

            If Appears = False Then
                .Range("C" & .Cells(.Rows.Count, "C").End(xlUp).Row + 1).Value = arrA(i)
            End If

        Next i

    End With

End Sub
Sub tesqt()

    Dim dictA As Object, dictB As Object
    Dim i As Long
    Dim cell As Range
    Dim key As Variant

    Set dictA = CreateObject("Scripting.Dictionary")
    Set dictB = CreateObject("Scripting.Dictionary")

    With ThisWorkbook.Worksheets("Sheet1")

        For Each cell In .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
            If Not dictA.Exists(cell.Value) Then
                dictA.Add key:=cell.Value, item:=dictA.Count + 1
            End If
        Next

        For Each cell In .Range("B2:B" & .Cells(.Rows.Count, "B").End(xlUp).Row)
            If Not dictB.Exists(cell.Value) Then
                dictB.Add key:=cell.Value, item:=dictB.Count + 1
            End If
        Next

        For Each key In dictA.keys

            If Not dictB.Exists(key) Then
                 .Range("C" & .Cells(.Rows.Count, "C").End(xlUp).Row + 1).Value = key
            End If

        Next

    End With

End Sub
用字典回答:

Option Explicit

Sub test()

    Dim LastRowC As Long
    Dim rngA As Range, rngB As Range, cell

    With ThisWorkbook.Worksheets("Sheet1")

        Set rngA = .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
        Set rngB = .Range("B2:B" & .Cells(.Rows.Count, "B").End(xlUp).Row)

        For Each cell In rngA

            If Application.WorksheetFunction.CountIf(rngB, cell) Then
            Else
                .Range("C" & .Cells(.Rows.Count, "C").End(xlUp).Row + 1).Value = cell.Value
            End If
        Next

    End With

End Sub
Option Explicit

Sub test()

    Dim LastRowC As Long
    Dim arrA As Variant, arrB As Variant, i As Long, y As Long
    Dim strA As String, strB As String
    Dim Appears As Boolean

    With ThisWorkbook.Worksheets("Sheet1")

        arrA = Application.Transpose(.Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row))
        arrB = Application.Transpose(.Range("B2:B" & .Cells(.Rows.Count, "B").End(xlUp).Row))

        For i = 1 To UBound(arrA)

            strA = arrA(i)

            Appears = False

            For y = 1 To UBound(arrB)

                strB = arrB(y)

                If strA = strB Then
                    Appears = True
                    Exit For
                Else
                    Appears = False
                End If
            Next y

            If Appears = False Then
                .Range("C" & .Cells(.Rows.Count, "C").End(xlUp).Row + 1).Value = arrA(i)
            End If

        Next i

    End With

End Sub
Sub tesqt()

    Dim dictA As Object, dictB As Object
    Dim i As Long
    Dim cell As Range
    Dim key As Variant

    Set dictA = CreateObject("Scripting.Dictionary")
    Set dictB = CreateObject("Scripting.Dictionary")

    With ThisWorkbook.Worksheets("Sheet1")

        For Each cell In .Range("A2:A" & .Cells(.Rows.Count, "A").End(xlUp).Row)
            If Not dictA.Exists(cell.Value) Then
                dictA.Add key:=cell.Value, item:=dictA.Count + 1
            End If
        Next

        For Each cell In .Range("B2:B" & .Cells(.Rows.Count, "B").End(xlUp).Row)
            If Not dictB.Exists(cell.Value) Then
                dictB.Add key:=cell.Value, item:=dictB.Count + 1
            End If
        Next

        For Each key In dictA.keys

            If Not dictB.Exists(key) Then
                 .Range("C" & .Cells(.Rows.Count, "C").End(xlUp).Row + 1).Value = key
            End If

        Next

    End With

End Sub
结果:


为什么不改用vlookup?这听起来像是一个例子。你已经在
CountIf
中翻转了参数顺序。第一个参数是范围,第二个是标准。@BigBen谢谢,请注意