简体   繁体   English

检查单元格是否包含数字

[英]Check if cell contains numbers

If the cell within the range mentioned in the code contains a number eg "12" the code works and the cell becomes empty.如果代码中提到的范围内的单元格包含一个数字,例如“12”,则代码起作用并且单元格变为空。

If the cell contains a number and text eg "12amasa" or "asa12" the code doesn't work.如果单元格包含数字和文本,例如“12amasa”或“asa12”,则代码不起作用。

I thought If IsNumeric(cell.Value) And cell.Value <> vbNullString Then would do the job, but it does not.我认为If IsNumeric(cell.Value) And cell.Value <> vbNullString Then可以完成这项工作,但事实并非如此。

I want if the cell contains a digit, then it should be empty.我想如果单元格包含一个数字,那么它应该是空的。 Only letters from az allowed.只允许来自 az 的字母。

Dim cell As Range
Application.EnableEvents = False

For Each cell In Target
    If Not Application.Intersect(cell, Range("a5:a10000")) Is Nothing Then
        If IsNumeric(cell.Value) And cell.Value <> vbNullString Then              
            cell.Value = vbNullString
            cell.Interior.Color = RGB(255, 0, 0)
        Else
            cell.Interior.Color = RGB(1000, 1000, 1000)
        End If
    End If
Next cell

Application.EnableEvents = True

You will need to loop through the characters checking for numeric values, or loop through the possible numeric values to see whether it is in the string.您将需要遍历检查数值的字符,或遍历可能的数值以查看它是否在字符串中。

Using the second method would be something like:使用第二种方法将类似于:

Dim cell As Range
Dim i As Long
Dim Matched As Boolean
Application.EnableEvents = False

For Each cell In Target
  If Not Application.Intersect(cell, Range("a5:a10000")) Is Nothing Then
     Matched = False
     For i = 0 To 9
       If Instr(CStr(cell.Value), CStr(i)) > 0 Then
         Matched = True
         Exit For
       End If
    Next
    If Matched Then              
      cell.Value = vbNullString
      cell.Interior.Color = RGB(255, 0, 0)
    Else
      cell.Interior.Color = RGB(1000, 1000, 1000)
    End If
  End If
Next cell

Application.EnableEvents = True

You can probably do this with a RegEx too, but I have never used them, so someone else will have to demonstrate that.你也可以用 RegEx 来做到这一点,但我从未使用过它们,所以其他人必须证明这一点。


And, of course, I overlooked the most obvious answer ... the Like operator:而且,当然,我忽略了最明显的答案…… Like运算符:

Dim cell As Range
Application.EnableEvents = False

For Each cell In Target
  If Not Application.Intersect(cell, Range("a5:a10000")) Is Nothing Then
    If CStr(cell.Value) Like "*[0-9]*" Then
      cell.Value = vbNullString
      cell.Interior.Color = RGB(255, 0, 0)
    Else
      cell.Interior.Color = RGB(1000, 1000, 1000)
    End If
  End If
Next cell

This can also be achieved using regular expressions, and much helpful in more complicated cases:这也可以使用正则表达式来实现,并且在更复杂的情况下很有帮助:

Dim RE As Object, RE2 As Object
Set RE = CreateObject("VBScript.RegExp")
RE.ignorecase = True
RE.Global = True

RE.Pattern = "[a-z]*\d+[a-z]*" 'Case is ignored in this pattern, see above Re.ignorecase = True

For Each cell In target
     If Not Application.Intersect(cell, Range("a5:a10000")) Is Nothing Then
          If RE.test(cell.Value) Then
               cell.Value = vbNullString
               cell.Interior.Color = RGB(255, 0, 0)
          Else
               cell.Interior.Color = RGB(1000, 1000, 1000)
          End If
     End If
Next cell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM