检查在Excel sheet单元格中的几个字符串中是否存在长度的字符串

Check if there exists in an Excel sheet a string of length within several strings in cell

我想看看某个单元格中是否存在长度为25的字符串。问题是我要检查的单元格包含几个由 space 分隔的字符串。在这些字符串中,我想知道是否有长度为25的字符串。

例如,如果我使用 LEN 函数,我会得到单元格中字符串的总长度,同时我想看看该单元格中的所有字符串之间是否确实存在长度为 25 的字符串。

您需要将字符串拆分为单独的字符串才能使用 Len,因此请使用 Split 并循环遍历字符串数组。这是单个单元格检查的代码:

Dim WrdArray() As String
Dim TestLen As Integer

' Get the cell and split it into separate strings in an array
WrdArray() = Split(Range("A1"))

' Loop the strings array to find any that equal 25 characters long
For i = LBound(WrdArray) To UBound(WrdArray)
    TestLen = Len(WrdArray(i))
    If (TestLen = 25) Then
        MsgBox "found one"
    End If
Next i