在 excel 2010 中,如果单元格包含“,”如何进行验证,然后向用户弹出一条消息?
问问题
1733 次
3 回答
1
请尝试展示您的作品..
假设 A 列包含数据,那么下面的代码可以完美运行
这就是你想要的(测试)
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
于 2013-10-07T04:07:37.830 回答
1
使用正常的数据验证,你可以试试这个
=(LEN(A1) = LEN(SUBSTITUTE(A1,",","")))
于 2013-10-07T04:15:07.567 回答
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
于 2013-10-07T05:20:16.573 回答