如何设置案例以省略任何小于X个字符的单词

时间:2016-12-21 21:35:03

标签: excel vba excel-vba

我正在使用以下循环。我有一个省略或blacklists certain words in VBA的案例。但是,我想省略任何少于3个字符的单词。

For i = 0 To UBound(words, 1)
    Select Case words(i)
        Case = "and","or","big","small","whatever else you want to add"
        Case Else
            For Each phrase In phrases
                If InStr(1, phrase.Value, words(i)) Then
                    matches = matches & phrase & "/"
                End If
            Next phrase
    End Select
Next i

任何人都知道该怎么做?我想Case => "[something for 3 characters]"与其他代码一起插入。{0}}您的积极投入和支持表示赞赏。

3 个答案:

答案 0 :(得分:2)

测试单词的长度:

For i = 0 To UBound(words, 1)
    If Len(words(i)) > 2 Then
        Select Case words(i)
            Case = "and", "big", "small", "whatever else you want to add"
            Case Else
                For Each phrase In phrases
                    If InStr(1, phrase.Value, words(i)) Then
                        matches = matches & phrase & "/"
                    End If
                Next phrase
        End Select
    End If
Next i

答案 1 :(得分:2)

-webkit-appearance: button;
-moz-appearance: button;
appearance: button;

答案 2 :(得分:1)

我回顾了您发布的原始链接。

我认为你最好删除Regexp所做的所有不必要的单词(你的黑名单少于3个字符),将白名单中的单词留在1D数组中,用于你的短语。 / p>

Dim StrIn As String
Dim objRegex As Object
Dim Words

StrIn = Join(Application.Transpose(ThisWorkbook.Worksheets("Topics").Range("D1:D3000")), Chr(32))

Set objRegex = CreateObject("vbscript.regexp")
With objRegex
  .Pattern = "\b([a-z]{1,2}|and|or|big|small)\b"
  .Global = True
  .ignorecase = True
StrIn = Application.Trim(.Replace(StrIn, vbNullString))
End With

Words = Split(StrIn, Chr(32))