0

有人可以告诉我如何检查一个单元格,A1 包含 2 个相同的数字吗?示例单元格 A1 = 1124。我的意思是包含 2 个相同的数字,如果是这样,B1 将是“是”非常感谢。

4

1 回答 1

1

如果您使用的是 Excel 2010(未在 Excel 2007 中测试),那么您不需要 VBA。您可以使用此公式B1并将其复制下来。

=IF(LOWER(A1)<>SUBSTITUTE(LOWER(A1),LOWER(1),"",2),"Yes",
 IF(LOWER(A1)<>SUBSTITUTE(LOWER(A1),LOWER(2),"",2),"Yes",
 IF(LOWER(A1)<>SUBSTITUTE(LOWER(A1),LOWER(3),"",2),"Yes",
 IF(LOWER(A1)<>SUBSTITUTE(LOWER(A1),LOWER(4),"",2),"Yes",
 IF(LOWER(A1)<>SUBSTITUTE(LOWER(A1),LOWER(5),"",2),"Yes",
 IF(LOWER(A1)<>SUBSTITUTE(LOWER(A1),LOWER(6),"",2),"Yes",
 IF(LOWER(A1)<>SUBSTITUTE(LOWER(A1),LOWER(7),"",2),"Yes",
 IF(LOWER(A1)<>SUBSTITUTE(LOWER(A1),LOWER(8),"",2),"Yes",
 IF(LOWER(A1)<>SUBSTITUTE(LOWER(A1),LOWER(9),"",2),"Yes",
 IF(LOWER(A1)<>SUBSTITUTE(LOWER(A1),LOWER(0),"",2),"Yes",
 "No"))))))))))

解释

该公式从0 - 9excel 单元格中检查该特定数字是否重复出现。

截屏

在此处输入图像描述

VBA 版本

Sub Sample()
    Debug.Print Does2NumbersMatch(Range("A1"))
End Sub

Function Does2NumbersMatch(rng As Range)
    Dim i As Long

    Does2NumbersMatch = "No"

    For i = 0 To 9
        isMatch = Application.Evaluate("=IF(LOWER(" & rng.Address & ")<>SUBSTITUTE(LOWER(" & _
                  rng.Address & "),LOWER(" & i & "),"""",2),""Yes"",""No"")")
        If isMatch = "Yes" Then
            Does2NumbersMatch = "Yes"
            Exit Function
        End If
    Next i
End Function
于 2013-04-15T15:25:58.500 回答