I’m completely lost and need some expert advice.
In excel I have a column full of keywords, and I want to be able to use a macro that adds a “+” before every word, with the exception of a set of words that don’t need it.
So basically in the VBA code there needs to be a place where I can say “skip the words ‘the, for, with,” etc.
Edit: I’ve only found code that allows to append to the beginning of every cell. So that’s close, but not exactly what I’m looking for. I couldn’t find anything closer to what I needed.
Sub Add_Plus()
Dim r As Range
With Selection
For Each r In Selection
r.Value = "+" & r.Value
Next
End With
End Sub
DOUBLE EDIT –
You guys are awesome! We’re super close, I just need the macro to add the + sign to every word in each cell, with the exception of the ones specified as exceptions. Thanks so much!
So the output we’re looking for would be this:
Cell 1 before macro: the happy dog jumps for joy
Cell after macro: the +happy +dog +jumps for +joy
That is how I understood your question- every word with the cell should have “+” except words which are excluded.
Try the following code. See some comments below
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
After you run the code the following cells:
do re mi
fa sol la
do place all
to words skip
here do re
place all
will be replace into:
+do +re +mi
+fa +sol +la
+do place all
to words skip
here +do +re
place all
Answer:
This can be done easily with a formula.
Assuming the “skip words” are in column A of Sheet2, then:
=IF(COUNTIF(Sheet2!A:A,A1)=0,"+","")&A1
will do it.
Answer:
Here you go. Just use some logic to determine when to skip the words:
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
Answer:
Select your cells and run this:
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
Change the exclusions to meet your needs.
Tags: excelexcel, vba