Excel 2010数据验证,以检查单元格是否包含逗号值

时间:2013-10-07 03:57:37

标签: excel excel-vba vba

在excel 2010中,如果单元格包含',',如何进行验证,然后向用户弹出消息?

3 个答案:

答案 0 :(得分:1)

请尝试展示你的作品..

让我们说列A包含数据,然后在代码下面完美地工作

这就是你想要的(TESTED)

Sub tested()
Dim erange As Range
Dim lrow As Integer

lrow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row

For Each erange In Range("A2:A" & lrow)

If InStr(erange.Value, ",") > 0 Then

MsgBox (erange.Address & " contains Comma ")

erange.Interior.Color = vbRed

End If

Next erange

End Sub

答案 1 :(得分:1)

使用普通数据验证,您可以试试这个

=(LEN(A1) = LEN(SUBSTITUTE(A1,",","")))

答案 2 :(得分:0)

如果你想避免在代码下面使用不必要的循环。

Sub findComma()

    Dim srcRng As Range, findRng As Range
    Dim firstCell As String

    Dim lrow As Integer

    lrow = Range("A" & Rows.Count).End(xlUp).Row
    Set srcRng = Range("A1:A" & lrow)
    Set findRng = srcRng.Find(What:=",", LookIn:=xlValues, LookAt:=xlPart)


    If Not findRng Is Nothing Then firstCell = findRng.Address

    Do Until findRng Is Nothing


        MsgBox (findRng.Address & " contains Comma ")
        findRng.Interior.Color = vbRed

        Set findRng = srcRng.FindNext(findRng)

        If findRng.Address = firstCell Then Exit Sub

    Loop

End Sub