1

我完全迷路了,需要一些专家建议。

中,我有一列充满了关键字,我希望能够使用一个宏,在每个单词之前添加一个“+”,但一组不需要它的单词除外。

所以基本上在 VBA 代码中需要有一个地方我可以说“跳过单词'the, for, with”等。

编辑:我只找到了允许附加到每个单元格开头的代码。所以这很接近,但不完全是我想要的。我找不到更接近我需要的东西。

Sub Add_Plus() 

Dim r As Range 

With Selection 

    For Each r In Selection 
        r.Value = "+" & r.Value 
    Next 

End With 

End Sub 

双重编辑 -

你们真棒!我们非常接近,我只需要宏将 + 号添加到每个单元格中的每个单词,但指定为例外的单词除外。非常感谢!

所以我们正在寻找的输出是这样的:

宏之前的单元格 1:快乐的狗高兴地跳起来 宏之后的单元格:+快乐的 +dog +jumps for +joy

4

4 回答 4

1

干得好。只需使用一些逻辑来确定何时跳过单词:

Sub Add_Plus()

Dim r As Range
Dim words As Variant
Dim word As Variant
Dim w As Long
Dim exclude As Variant
'Allow user-input to capture a list/array of words to exclude:
exclude = Split(Replace(Application.InputBox("Enter a comma-separated list of words to exclude"), " ", vbNullString), ",")
    For Each r In Selection.Cells
        'Create an array of "words" from the cell:
        words = Split((r.Value), " ")
        w = LBound(words)
        ' iterate over each "word"
        For Each word In words
            'Make sure the word is not in the excluded list
            If Not IsError(Application.Match(word, exclude, False)) Then
                '    DO NOTHING
            Else
                ' Add the "+" to these words:
                     words(w) = "+" & word
            End If
            w = w + 1
        Next
        r.Value = Join(words, " ")
   Next

End Sub
于 2013-10-17T13:49:27.967 回答
1

这可以通过公式轻松完成。

假设“跳过词”在 Sheet2 的 A 列中,则:

=IF(COUNTIF(Sheet2!A:A,A1)=0,"+","")&A1

会做的。

于 2013-10-17T13:55:38.977 回答
1

这就是我理解你的问题的方式——单元格中的每个单词都应该有“+”,除了被排除的单词。

试试下面的代码。请参阅下面的一些评论

Sub Add_Plus()

Dim r As Range
Dim SkipWords As Variant
    'put all excluded word here, separate, within quotation marks
    SkipWords = Array("place", "all", "words", "to", "skip", "here")


Dim tmpWords As Variant
Dim i As Integer, j As Integer
Dim Final As String, boExclude As Boolean
    For Each r In Selection
        tmpWords = Split(r.Value, " ")
            For i = 0 To UBound(tmpWords)
                For j = 0 To UBound(SkipWords)
                    If UCase(SkipWords(j)) = UCase(tmpWords(i)) Then
                        'do nothing, skip
                        boExclude = True
                        Exit For
                    End If
                Next j
                If boExclude = False Then
                    Final = Final & "+" & tmpWords(i) & " "
                Else
                    'this will keep excluded words without "+"
                     'remove it if you want to remove the excluded words
                    Final = Final & tmpWords(i) & " "
                End If
                boExclude = False
            Next i
            r = Left(Final, Len(Final) - 1)
            Final = ""
    Next

End Sub

运行代码后,以下单元格:

do re mi
fa sol la
do place all
to words skip
here do re
place all

将被替换为:

+do +re +mi 
+fa +sol +la 
+do place all 
to words skip 
here +do +re 
place all 
于 2013-10-17T14:00:27.257 回答
0

选择您的单元格并运行:

Sub dural()
    For Each r In Selection
    If r.Value <> "happy" And r.Value <> "sad" Then
        r.Value = "+" & r.Value
    End If
    Next
End Sub

更改排除项以满足您的需求。

于 2013-10-17T13:47:43.723 回答