查找两个不均匀变量数组之间的匹配项

问题描述 投票:0回答:2

我需要检查arr1中的每个字符串在arr2中是否都有匹配项。

如果有匹配项,则在arr1旁边的列中输入“匹配项”,如果没有,则为“不匹配”。

这是我的工作表的外观:

How my Sheet looks

Sub Variant_Array_Question()
'Here is my frankenstein monster of a code
    Dim DocNm As Variant, NroNm As Variant
    Dim i As Long, j As Long
    Dim NroLastRow As Long, DocLastRow As Long

    'Arr1
    DocLastRow = ShStart.Range("Q" & Rows.Count).End(xlUp).Row
    DocNm = ShStart.Range("Q6:Q" & DocLastRow).Value

    'Arr2
    NroLastRow = ShStart.Range("T" & Rows.Count).End(xlUp).Row
    NroNm = ShStart.Range("T6:T" & NroLastRow).Value

    For i = 1 To UBound(DocNm)
        For j = 1 To UBound(NroNm)
            If DocNm(i, 1) = NroNm(j, 1) Then
                'Match was found ==== Run into Problem here
                DocNm(i, 1).Offset(0, 1).Value = "Match"
                Exit For
            End If
        Next j
        If i > UBound(NroNm) Then
            'No match was found ==== Run into Problem here
            DocNm(i, 1).Offset(0, 1).Value = "Not Match"
        End If
    Next i
End Sub
excel vba match variant-array
2个回答
0
投票

DocNm(i, 1).Offset(0, 1).Value = "Match"对数组没有意义。数组没有Offset属性。

如果您的工作表示例在范围大小方面是真实的,请使用Ranges而不是数组。

为了使用数组并获得结果,您必须使用第三个数组。将其标注为您的第一个数组Ubound,但我会更好地转换您的代码:

Sub Variant_Array_Question()
    Dim DocNm As Variant, NroNm As Variant, arrStat As Variant
    Dim i As Long, j As Long, boolFound As Boolean
    Dim NroLastRow As Long, DocLastRow As Long
    Dim ShStart As Worksheet
    Set ShStart = ActiveSheet 'use here your sheet!!!
    'Arr1
    DocLastRow = ShStart.Range("Q" & Rows.Count).End(xlUp).Row
    DocNm = ShStart.Range("Q6:Q" & DocLastRow).value
    ReDim arrStat(1 To UBound(DocNm, 1), 1 To 1) 'arr 3
    'Arr2
    NroLastRow = ShStart.Range("T" & Rows.Count).End(xlUp).Row
    NroNm = ShStart.Range("T6:T" & NroLastRow).value

    For i = 1 To UBound(DocNm)
        For j = 1 To UBound(NroNm)
            If DocNm(i, 1) = NroNm(j, 1) Then
                boolFound = True
                arrStat(i, 1) = "Match"
                Exit For
            End If
        Next j
        If Not boolFound Then
            arrStat(i, 1) = "Not Match"
        End If
        boolFound = False
    Next i
    ShStart.Range("R6").Resize(UBound(arrStat, 1), 1).value = arrStat
End Sub

[未经测试,但我认为它将起作用。如果您提供一个可编辑的示例,我将对其进行测试...


0
投票

这是FaneDuru帮助我解决的代码。这适用于像我这样需要帮助的菜鸟。

Sub Variant_Array_Response()
    Dim DocNm As Variant, NroNm As Variant, ResStatus As Variant
    Dim i As Long, j As Long, boolFound As Boolean
    Dim NroLastRow As Long, DocLastRow As Long
    Dim ShStart As Worksheet

    Set ShStart = ActiveSheet

    'Arr1
    DocLastRow = ShStart.Range("Q" & Rows.Count).End(xlUp).Row
    DocNm = ShStart.Range("Q6:Q" & DocLastRow).Value

    'Arr2
    NroLastRow = ShStart.Range("T" & Rows.Count).End(xlUp).Row
    NroNm = ShStart.Range("T6:T" & NroLastRow).Value

    'Arr3
    'ResStatus = ShStart.Range("Q6:Q" & DocLastRow).Offset(, 1) 'What I had
    ReDim ResStatus(1 To UBound(DocNm, 1), 1 To 1) 'FaneDuru's pice

    For i = 1 To UBound(DocNm)
        For j = 1 To UBound(NroNm)
            If DocNm(i, 1) = NroNm(j, 1) Then
                boolFound = True
                ResStatus(i, 1) = "Match"
                Exit For
            End If
        Next j

        If Not boolFound Then
            ResStatus(i, 1) = "Not Match"
        End If
        boolFound = False
    Next i
    ShStart.Range("R6").Resize(UBound(ResStatus, 1), 1).Value = ResStatus
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.