简体   繁体   English

用于检查a-z的字符串的函数

[英]Function to check string for a-z

I want to : 我想要 :

  • cycle down a specific column H (starting from H4 ), and 循环一个特定的列H (从H4开始),和
  • for each cell along that column, call a function to see if it is true (then do something ) or if it comes out false (do something else). 对于该列中的每个单元格,调用一个函数来查看它是否为真(然后执行某些操作)或者它是否为false(执行其他操作)。

I'm getting runtime error ***invalid use of property*** on Call Isletter . 我收到运行时错误***invalid use of property***Call Isletter***invalid use of property***

Sub IfBlank()

Dim Rng As Range
Dim MyCell As Range
Dim Isletter As Range

Set Rng = Range("H4:H" & Cells(Rows.Count, "H").End(xlUp).Row)

For Each MyCell In Rng
Call Isletter
If Isletter(MyCell.Value) = True Then
'do nothing
End If
If Isletter(MyCell.Value) = False Then
 MyCell.Value = "-"
 End If

 Next MyCell
End Sub

Public Function IsLetter(MyCell As String) As Boolean

    Dim intPos As Integer
    For intPos = 1 To Len(MyCell)
        Select Case Asc(Mid(MyCell, intPos, 1))
            Case 33 To 127
                Isletter = True
            Case Else
                Isletter = False
                Exit For
        End Select
    Next
End Function

IsLetter function has non optional argument (MyCell As String) so you must always pass this argument and must be string. IsLetter函数具有非可选参数(MyCell As String)因此您必须始终传递此参数,并且必须是字符串。

If IsLetter(MyCell.Value) = True Then
  'do something
Else
  'do something else  
End If

The code below 下面的代码

  • uses a variant array (which is much quicker than a range loop) to process each value from H4:Hx 使用变量数组(比范围循环快得多)来处理来自H4:Hx的每个值
  • uses a that can quickly check if at least one character in the string is alphabetic. 使用正则 ,可以快速检查字符串中是否至少有一个字符是字母。 If so blanks that string. 如果是那么空白那个字符串。

Thos line Range("H4:H" & Cells(Rows.Count, "H").End(xlUp).Row).Value2 = X writes the changed array back to the range. Thos行Range("H4:H" & Cells(Rows.Count, "H").End(xlUp).Row).Value2 = X将更改后的数组写回范围。

code

Sub IfBlank()

Dim rng1 As Range
Dim X
Dim lngCnt As Long

Dim objRegex As Object
X = Range("H4:H" & Cells(Rows.Count, "H").End(xlUp).Row).Value2

Set objRegex = CreateObject("vbscript.regexp")

    With objRegex
         .IgnoreCase = True
         .Pattern = "[a-z]"

         For lngCnt = 1 To UBound(X)
            If .test(X(lngCnt, 1)) Then X(lngCnt, 1) = vbNullString
         Next
    End With

Range("H4:H" & Cells(Rows.Count, "H").End(xlUp).Row).Value2 = X

End Sub

The "Call isletter" shouldn't be there That part of the code should look like this instead 不应该存在“Call isletter”那部分代码应该是这样的

For Each MyCell In Rng
    If Isletter(MyCell.Value) = True Then
     'do nothing
    else
     MyCell.Value = "-"
    End If

Next MyCell

Your "IsLetter" function is going to cause trouble. 你的“IsLetter”功能会带来麻烦。 ASCII 127 is "delete" ASCII 127是“删除”

Also, only ASCII 65-90 and 97 - 122 are letters. 此外,只有ASCII 65-90和97 - 122是字母。 Is this supposed to include numeric and special characters? 这应该包括数字和特殊字符吗?

if not, then it should look more like this 如果没有,那么它应该看起来更像这样

Public Function IsLetter(MyCell As String) As Boolean

Dim intPos As Integer
For intPos = 1 To Len(MyCell)
    Select Case Asc(Mid(Ucase(MyCell), intPos, 1))
        Case 90 To 122
            Isletter = True
            Exit Function
        Case Else
            Isletter = False
            Exit For
    End Select
Next
End Function

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

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