Reputation: 5522
In a VB macro for Excel, how do I remove all occurances of a word that starts with a certain string?
Eg:
The string reads: xxxx $AUD543.43 yyyy
I want to search for anything in a string that starts with $AUD
and remove the whole word before the next space
So the example above should result in:
xxxx yyyy
Upvotes: 1
Views: 5870
Reputation: 1
My example is used for delete the last few words of a cell
For example:
in cell A1: ABCDE[Acct:12345]
in cell A2: FGHIJ[Acct:67890]
in cell A3: KLMNO
Wanna delete all the words begin with "[Acct:", note not every cell includes "[Acct:"
Now in column B, insert the below function, this will return with the words on the left on "[Acct:" =LEFT(A1,FIND("[Acct:",A1)-1)
Result: A B ABCDE[Acct:12345] ABCDE FGHIJ[Acct:67890] FGHIJ KLMNO #VALUE! Now in column C, insert below function, this will check whether column B is #VALUE! and then return with what we want =IF(ISERROR(C1)=TRUE,A1,B1))
Result:
A B C
ABCDE[Acct:12345] ABCDE ABCDE
FGHIJ[Acct:67890] FGHIJ FGHIJ
KLMNO #VALUE! KLMNO
Then you can copy the three columns and paste with value, and delete column A and B , column C will be the final result you want.
Upvotes: 0
Reputation: 2501
use Regex. Add a reference to Microsoft VBScript Regular Expressions in VBA >> Tools >> Options.
Dim txt As String
txt = "$Audthisfew is$Aud $Auda test $Aud"
Set regEx = New RegExp
With regEx
.Global = True
.Pattern = "((^, )\$Aud)"
Debug.Print .Replace(txt, "")
End With
Upvotes: 2
Reputation: 17475
Although not exactly what you ask for, but you could also use an Excel formula to achieve this. Assuming your text is in A1, the formula would be:
=TRIM(LEFT(A1,FIND("$AUD",A1)-1))&RIGHT(A1,LEN(A1)-FIND(" ",A1,FIND("$AUD",A1))+1)
Upvotes: 1
Reputation: 2017
It would be remiss of me not to remind you of the expectation that you show "what you (have) tried". (Which I do to avoid me being yelled at for answering the question.) My duty thus having been done, I now move on to it.
You actually don't necessarily need VBA code to do this; you could do it with the Find() function albeit more clumsily and I wouldn't recommend it for a really large sheet. Still, VBA code you have specified, and that you shall have. Change the range to match the one that you'll be searching. ALSO, you should note that you have only one space between the x's and y's in your example but that varies from your request that it be the word starting with $AUD and ending BEFORE the next space. If you want only one space, please adjust the formulas accordingly.
Sub ReplaceText()
Dim rng As Excel.Range
Dim s_Contents As String
Dim l_FindAUD As Long, l_FindSpace As Long
For Each rng In ActiveSheet.UsedRange
s_Contents = rng.Value
'Does the $AUD expression exist in this cell?
l_FindAUD = InStr(1, s_Contents, "$AUD", vbTextCompare)
If l_FindAUD > 0 Then
'If so, is it followed by a space?
l_FindSpace = InStr(l_FindAUD, s_Contents, " ")
If l_FindSpace > 0 Then
'If so, take all of the content up to but not including the $
'and all of the contents from the space onwards, merge them
'together and write to the cell.
s_Contents = Left$(s_Contents, l_FindAUD - 1) & Mid$(s_Contents, l_FindSpace)
rng.Value = s_Contents
End If
End If
Next
End Sub
Upvotes: 1