【问题标题】:vba: Compare columns and return mismatched valuesvba:比较列并返回不匹配的值
【发布时间】:2017-10-30 22:01:00
【问题描述】:

我是 VBA 的新手。 我的问题是:

我有 3 张纸(1、2 和 3)。在sheet 1,我有A列(范围A2-end)与我想与sheet 2上的A列(范围A2-end)和D(范围D2-end)进行比较的数据。如果在 sheet 2 列 A 和 D 上未找到 sheet 1 列 A 中的值,则它应从范围 A2 开始在工作表 3 中列出 mismatched 值。

这是我所拥有的:

Sub Makro5()

Dim lastRowE As Integer
Dim lastRowF As Integer
Dim lastRowM As Integer
Dim foundTrue As Boolean


Application.ScreenUpdating = False

lastRowE = Sheets("1").Cells(Sheets("1").Rows.Count, "A2").End(xlUp).row
lastRowE = Sheets("2").Cells(Sheets("2").Rows.Count, "A2").End(xlUp).row
lastRowF = Sheets("2").Cells(Sheets("2").Rows.Count, "D2").End(xlUp).row
lastRowM = Sheets("3").Cells(Sheets("3").Rows.Count, "A2").End(xlUp).row


For i = 1 To lastRowE
foundTrue = False
For j = 1 To lastRowF

    If Sheets("1").Cells(i, 1).value = Sheets("2").Cells(j, 1).value Then
        foundTrue = True
and
    If Sheets("1").Cells(i, 1).value = Sheets("2").Cells(j, 4).value Then
        foundTrue = True

        Exit For
    End If

Next j

If Not foundTrue Then

    Sheets("3").Rows(i).Copy Destination:= _
    Sheets("3").Rows(lastRowM + 1)
    lastRowM = lastRowM + 1

End If

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    这从字典中的 Sheet2 中读取 col A 和 D

    然后在字典中搜索 Sheet1 的 col A 中的值

    未找到的项目放置在 Sheet3 中,从单元格 A2 开始


    Option Explicit
    
    Public Sub FindMissing()
      Dim ws1 As Worksheet, colA1 As Variant, r As Long, d1 As Object, d2 As Object
      Dim ws2 As Worksheet, colA2 As Variant, colD2 As Variant, ws3 As Worksheet
    
      Set d1 = CreateObject("Scripting.Dictionary")
      Set d2 = CreateObject("Scripting.Dictionary")
    
      Set ws1 = ThisWorkbook.Worksheets("Sheet1")
      Set ws2 = ThisWorkbook.Worksheets("Sheet2")
      Set ws3 = ThisWorkbook.Worksheets("Sheet3")
    
      colA1 = ws1.Range("A2:A" & ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Row)    'Sheet1.colA
      colA2 = ws2.Range("A2:A" & ws2.Cells(ws2.Rows.Count, 1).End(xlUp).Row)    'Sheet2.colA
      colD2 = ws2.Range("D2:D" & ws2.Cells(ws2.Rows.Count, 4).End(xlUp).Row)    'Sheet2.colD
    
      If Not IsArray(colA1) Then MakeArray colA1  'Sheet1.colA contains only 1 row
      If Not IsArray(colA2) Then MakeArray colA2  'Sheet2.colA contains only 1 row
      If Not IsArray(colD2) Then MakeArray colD2  'Sheet2.colD contains only 1 row
    
      For r = 1 To UBound(colA2)
        d1(colA2(r, 1)) = vbNullString  'read Sheet2.ColA in dictionary d1.Keys
      Next
      For r = 1 To UBound(colD2)
        d1(colD2(r, 1)) = vbNullString  'read Sheet2.ColD in dictionary d1.Keys
      Next
    
      For r = 1 To UBound(colA1)        'search vals from Sheet1.colA in dictionary d1
        If Not d1.Exists(colA1(r, 1)) Then d2(colA1(r, 1)) = vbNullString
      Next
    
      ws3.Columns(1).Delete
      If d2.Count > 0 Then ws3.Cells(2, 1).Resize(d2.Count, 1) = Application.Transpose(d2.Keys)
    End Sub
    

    Private Sub MakeArray(ByRef arr As Variant)
       Dim tmp As Variant
       tmp = arr
       ReDim arr(1 To 1, 1 To 1)
       arr(1, 1) = tmp
    End Sub
    

    【讨论】:

    • 当我在工作表 1 中的输入数据,范围 A2:A 仅包含 1 行时,则此行调试“For r = 2 To UBound(colA1)”从字典 d1 中的 Sheet1.colA 中搜索 vals ”。我得到运行时错误“13”类型不匹配。你知道有没有办法解决这个问题?
    • 我更新了代码以检查所有数组 - 如果任何一列仅包含一行,则该变量将转换为二维数组
    【解决方案2】:

    尝试使用下面的代码...

    Public Function Find_First(FindString As String, WithinRange As Range) As Boolean
    
        Dim rng As Range
        Find_First = False
        If Trim(FindString) <> "" Then
            With WithinRange
                Set rng = .Find(What:=FindString, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
                If Not rng Is Nothing Then
                    Find_First = True
                End If
            End With
        End If
    
    End Function
    

    【讨论】:

    • 不用担心,如果这有帮助,请将答案标记为“通过单击我的答案左侧的复选标记来回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 2015-08-04
    相关资源
    最近更新 更多