比较两组Ranges和MsgBox的数量和普通单元格值的值

Compare two sets of Ranges and MsgBox amount and value of common cell values

这段代码有问题,我无法解决。

我有两组范围; Range("C14:C19") 和 Range("E14:E19") 每个包含 6 个随机生成的数字(我已经编码了随机选择)。

我的目标是在生成数字后比较两个范围。 如果两个范围包含相同的数字, 我想向 MsgBox 发送在两个范围内找到的相同数字的数量以及每个数字的值。

示例: 生成数字后,如果两个范围都具有相同的值 9 和 7,则 MsgBox 将如下所示:

中奖组合中有 is/are 2 个号码,它的s/there 值 is/are : 7, 9

这是我到目前为止能够做的事情。

Private Sub Compare()

    Dim rgnChoix As Range, rgnTirage As Range, i As Range, j As Range, iVal As Integer

    Set rgnChoix = Range("C14:C19")
    Set rgnTirage = Range("E14:E19")

        For Each i In rgnChoix.Cells

            For Each j In rgnTirage.Cells

                If i.value = j.value Then

                    MsgBox "There is/are" & " " & iVal & " " & "number(s) in the winning combination and it's/there value(s) is/are :" & " " & i.value, , "Erika"
                        Exit Sub

                 End If
             Next j
          Next i

  iVal = Application.WorksheetFunction.CountIf(Range("C14:C19"),"i.value=j.value")

End Sub

谢谢!! 埃里卡

它可能会如您所愿。但是,如果相同的数字在一个范围内出现多次,则将其视为单独的匹配项。它分别计算所有匹配项。如果一个范围包含两个 4,而另一个范围包含三个 4,则计为六个匹配。

Private Sub Compare()
    Dim rgnChoix As Range, rgnTirage As Range, i As Range, j As Range, iVal As Integer, sVal As String
    Set rgnChoix = Range("C14:C19")
    Set rgnTirage = Range("E14:E19")
    For Each i In rgnChoix.Cells
        For Each j In rgnTirage.Cells
            If i.Value = j.Value Then
                iVal = iVal + 1
                sVal = sVal & "," & CStr(i.Value)
            End If
        Next j
    Next i
    sVal = Mid(sVal, 2)
    MsgBox "There is/are" & " " & iVal & " " & "number(s) in the winning combination and it's/there value(s) is/are :" & " " & sVal, , "Erika"
End Sub