display inside a column the number from a string - excel

I have one column (B) ( over 3k cells ):
TestUrl
__________________________________
http://www.testing.eu/test123.html
http://www.testing.eu/test154.html
http://www.testing.eu/test983.html
.. and so on ...
I want in column C, to get only the numbers:
Numbers
__________
123
154
983
..and so on..
How can I achieve this?

using an array formula..
assuming your data starts at B2.
type this in the formula bar on C2.
Then instead of pressing ENTER, press CTRL+SHIFT+ENTER.
Then just drag down to copy to other cells below.
=SUMPRODUCT(MID(0&B2,LARGE(INDEX(ISNUMBER(--MID(B2,ROW($1:$50),1))* ROW($1:$50),0),ROW($1:$50))+1,1)*10^ROW($1:$50)/10)
change the B2 in the formula if your data starts somwhere else. Just make sure to press CTRL+SHIFT+ENTER instead of ENTER after changing.
got this from:
http://www.extendoffice.com/documents/excel/1622-excel-extract-number-from-string.html

Using formula in column B:
=MID(A1,LEN("http://www.testing.eu/test") + 1, LEN(A1) - LEN(".html") - LEN("http://www.testing.eu/test"))
Using VBA and Regex:
Sub GetNumberFromURL()
Dim regEx As Object, matches As Object, cl As Range
Set regEx = CreateObject("vbscript.regexp")
regEx.Pattern = "([0-9]+)"
For Each cl In Range("A1:A10") //Update to suit your needs
Set matches = regEx.Execute(cl)
cl.Offset(0, 1) = matches.Item(0)
Next
End Sub

Copy the B column data in notepad and replace http://www.testing.eu/test by empty string and then do same thing with .html. After replacing/removing these string paste the notepad content in column C

Related

Use a Text Based Formula from one cell as the actual formula in another [duplicate]

I have 0,4*A1 in a cell (as a string). How can convert this "string formula" into a real formula and calculate its value, in another cell?
Evaluate might suit:
http://www.mrexcel.com/forum/showthread.php?t=62067
Function Eval(Ref As String)
Application.Volatile
Eval = Evaluate(Ref)
End Function
I concatenated my formula as normal, but at the start I had '= instead of =.
Then I copy and paste as text to where I need it. Then I highlight the section saved as text and press ctrl + H to find and replace.
I replace '= with = and all of my functions are active.
It's a few steps, but it avoids VBA.
UPDATE This used to work (in 2007, I believe), but does not in Excel 2013.
EXCEL 2013:
This isn't quite the same, but if it's possible to put 0.4 in one cell (B1, say), and the text value A1 in another cell (C1, say), in cell D1, you can use =B1*INDIRECT(C1), which results in the calculation of 0.4 * A1's value.
So, if A1 = 10, you'd get 0.4*10 = 4 in cell D1. I'll update again if I can find a better 2013 solution, and sorry the Microsoft destroyed the original functionality of INDIRECT!
EXCEL 2007 version:
For a non-VBA solution, use the INDIRECT formula. It takes a string as an argument and converts it to a cell reference.
For example, =0.4*INDIRECT("A1") will return the value of 0.4 * the value that's in cell A1 of that worksheet.
If cell A1 was, say, 10, then =0.4*INDIRECT("A1") would return 4.
Just for fun, I found an interesting article here, to use a somehow hidden evaluate function that does exist in Excel. The trick is to assign it to a name, and use the name in your cells, because EVALUATE() would give you an error msg if used directly in a cell. I tried and it works! You can use it with a relative name, if you want to copy accross rows if a sheet.
I prefer the VBA-solution for professional solutions.
With the replace-procedure part in the question
search and replace WHOLE WORDS ONLY, I use the following VBA-procedure:
''
' Evaluate Formula-Text in Excel
'
Function wm_Eval(myFormula As String, ParamArray variablesAndValues() As Variant) As Variant
Dim i As Long
'
' replace strings by values
'
For i = LBound(variablesAndValues) To UBound(variablesAndValues) Step 2
myFormula = RegExpReplaceWord(myFormula, variablesAndValues(i), variablesAndValues(i + 1))
Next
'
' internationalisation
'
myFormula = Replace(myFormula, Application.ThousandsSeparator, "")
myFormula = Replace(myFormula, Application.DecimalSeparator, ".")
myFormula = Replace(myFormula, Application.International(xlListSeparator), ",")
'
' return value
'
wm_Eval = Application.Evaluate(myFormula)
End Function
''
' Replace Whole Word
'
' Purpose : replace [strFind] with [strReplace] in [strSource]
' Comment : [strFind] can be plain text or a regexp pattern;
' all occurences of [strFind] are replaced
Public Function RegExpReplaceWord(ByVal strSource As String, _
ByVal strFind As String, _
ByVal strReplace As String) As String
' early binding requires reference to Microsoft VBScript
' Regular Expressions:
' with late binding, no reference needed:
Dim re As Object
Set re = CreateObject("VBScript.RegExp")
re.Global = True
're.IgnoreCase = True ' <-- case insensitve
re.Pattern = "\b" & strFind & "\b"
RegExpReplaceWord = re.Replace(strSource, strReplace)
Set re = Nothing
End Function
Usage of the procedure in an excel sheet looks like:
In my opinion the best solutions is in this link:
http://www.myonlinetraininghub.com/excel-factor-12-secret-evaluate-function
Here is a summary:
In cell A1 enter 1,
In cell A2 enter 2,
In cell A3 enter +,
Create a named range, with =Evaluate(A1 & A3 & A2) in the refers to field while creating the named range. Let's call this named range "testEval",
In cell A4 enter =testEval,
Cell A4 should have the value 3 in it.
Notes:
a) Requires no programming/VBA.
b) I did this in Excel 2013 and it works.
Say, let we have column E filled by formulas that returns string, like:
= " = " & D7
where D7 cell consist more complicated formula, that composes final desired result, say:
= 3.02 * 1024 * 1024 * 1024
And so in all huge qty of rows that are.
When rows are a little - it just enough to copy desired cells as values (by RMB)
to nearest column, say G, and press F2 with following Enter in each of rows.
However, in case of huge qty of rows it's impossible ...
So, No VBA. No extra formulas. No F&R
No mistakes, no typo, but stupid mechanical actions instead only,
Like on a Ford conveyor. And in just a few seconds only:
[Assume, all of involved columns are in "General" format.]
Open Notepad++
Select entire column D
Ctrl+C
Ctrl+V in NPP
Ctrl+A in NPP
Select cell in the first row of desired column G1
Ctrl+V
Enjoy :)
.
Excel 2019 here. EVALUATE isn't valid.
It would work if we created a Named Range out of it:
But in this case we provide an absolute reference, which is not nice:
We would have to modify the formula every time we want to reuse it.
When the value in A1 changes, the evaluated result would not change.
Solution:
=EVALUATE(GET.CELL(5,OFFSET(INDIRECT("RC",FALSE),0,-1)))
The best, non-VBA, way to do this is using the TEXT formula. It takes a string as an argument and converts it to a value.
For example, =TEXT ("0.4*A1",'##') will return the value of 0.4 * the value that's in cell A1 of that worksheet.

How can I get the length of the longest string in a column (Excel)?

I have an Excel spreadsheet that I am writing out to a file. I am currently using the following method to reproduce the contents of each cell:
cell_contents = Right(Space(10) & Trim(Cells(iRow, iColumn)), 10)
However, if the contents of the cell are longer than 10 characters long (or however long I choose to specify), then I loose parts of the data. Is there a way to quickly and easily get the length of the longest string in a range of cells?
I have seen people suggest using the following loop, but I was hoping that I might be able to do it without having to loop over all the cells:
For Each c In SourceRange.Cells
if len(c) > b then b = len(c)
Next c
Record these steps as a macro in the cell you want to calculate the max length.
1) enter this formula
=MAX(LEN(A1:A4), 1) -- Edit for your range.
2) press control-shift enter (FormulaArray)
3) now stop recording macro
4) view the VBA. copy what you need to your VBA.
Should give you something like this:
Selection.FormulaArray = "=MAX(LEN(R[-10]C[1]:R[-1]C[1]))"
Press Alt-F11, insert new module, paste code bellow.
Public Function maxRangeLength(data As Range) As Integer
Dim ret As Integer
ret = 0
For Each cell In data
ret = Application.Max(ret, Len(cell))
Next cell
maxRangeLength = ret
End Function
Usage:
=maxRangeLength(A8:D11)
Could just use ... {=MAX(LEN(A2:A200))}
In the top cell put =MAX(LEN(A2:A200)) .. and then press CTRL-SHIFT-ENTER to get the curly braces.
Suppose that the data is in column A and there is a heading row for your spreadsheet.
Create a new column next to it and enter in the formula: =LEN(A2)
Autofill down that formula.
Turn on filtering for that column and the click the drop-down box on the column heading cell.
Scroll down to the bottom of the list where the largest summarized value is.
That will be the largest value in the column.

How can I pull hashtags out of a text column?

I have an Excel sheet in which there is a "description" column. The values in this column often contain anywhere from 0-3 tags, all starting with the # symbol. Is there a way to pull all of these tags out in to columns?
Perhaps just have 3 blank columns called hashtag 1, 2, 3 and pull them in to each column.
It isn't even important that it remove them from the description column while pulling them out.
Example of descriptions:
"#0034 #lost client lost file" - pull out 0034 and lost
"worker has bad quality #SusanB #quality" - pull out SusanB and quality
"#0840 client complaint" - pull out 0840
"lots of ipsum" - pull out nothing
Lets say Column A is Description column, and in A2 you have the first cell with hashtags
In B2 enter:
=MID(A2;(FIND("#";A2))+1;(FIND(" ";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2)))-(FIND("#";A2))-1)
In C2 enter:
=MID(A2;(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2)))+1;(FIND(" ";MID(A2;(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2)))+1;LEN(A2)-(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2)))))+(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2))))-(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2)))-1)
In D2 enter:
=MID(A2;(FIND("#";MID(A2;(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2)))+1;LEN(A2)-(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2)))))+(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2))))+1;(FIND(" ";MID(A2;(FIND("#";MID(A2;(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2)))+1;LEN(A2)-(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2)))))+(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2))))+1;LEN(A2)-(FIND("#";MID(A2;(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2)))+1;LEN(A2)-(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2)))))+(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2))))))+(FIND("#";MID(A2;(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2)))+1;LEN(A2)-(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2)))))+(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2)))))-(FIND("#";MID(A2;(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2)))+1;LEN(A2)-(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2)))))+(FIND("#";MID(A2;(FIND("#";A2))+1;LEN(A2)-(FIND("#";A2))))+(FIND("#";A2))))-1)
I'm fond of an extension that can let you use REGEX in Excel ...
Without this :
1) find the position of the separator character (# ?) in your string with FIND()
2) then use LEFT(), MID() and RIGHT() to explode your string into 3 columns
3) you can delete the # using MID() instead of LEFT() and RIGHT()
--
It would be something like this for the first tag with the # :
=LEFT(A1,FIND("#",A1)-1)
--
Hope this will help !
This can always be done using regular expression.
In VBE, write following function in a module:
Function getHashTags(rng As Range) As Variant
Dim regEx As RegExp
Set regEx = New RegExp
regEx.Pattern = "#\w*\b"
regEx.IgnoreCase = True
regEx.Global = True
Set myMatches = regEx.Execute(rng.Value)
Dim arr(1 To 1, 1 To 3) As Variant
For i = 1 To 3
If i > myMatches.Count Then
arr(1, i) = ""
Else
arr(1, i) = Replace(myMatches(i - 1), "#", "")
End If
Next i
getHashTags = arr
End Function
Now, let's suppose Column A is the Description column, and in cell A2 you have the first cell with hash tags.
In cell B2 enter this:
=getHashTags(B$2)
Now select the cells B2, C2, D2, Press F2 and then ctrl+shift+enter. This will populate the variant return from the function getHashTags to the selected cells.
I hope this helps.
PS: And, yes for this to work you also need to give reference to Microsoft VBScript Regular Expressions 5.5 library.

Batch string concatenation in Excel

I have a couple hundred of cells in Excel I would like to concatenate into a single string. Is there a simpler method of doing this than going through them one by one manually in order to type them into CONCATENATE(A1, A2, ....)?
CONCATENATE(A1:A255) does not work.
*In a new tab, type A1 in cell A1,
*Type A2 in Cell A2
*Use fill series to complete the values in column A
*Type A1 in cell B1
Use this forumal in cell B2
=B1&","&A2
Copy the formula down.
Copy and paste values to harvest the string of values you created.
A1 A1
A2 A1,A2
A3 A1,A2,A3
A4 A1,A2,A3,A4
A5 A1,A2,A3,A4,A5
A6 A1,A2,A3,A4,A5,A6
A7 A1,A2,A3,A4,A5,A6,A7
A8 A1,A2,A3,A4,A5,A6,A7,A8
A9 A1,A2,A3,A4,A5,A6,A7,A8,A9
A10 A1,A2,A3,A4,A5,A6,A7,A8,A9,A10
Press Alt-F11, insert new module, paste code bellow.
Public Function concatRange(data As Range, Optional sep As String = "") As String
Dim ret As String
Dim sep2 As String
ret = ""
sep2 = ""
For Each cell In data
ret = ret & sep2 & cell.Value
sep2 = sep
Next cell
concatRange = ret
End Function
Usage:
=concatRange(A8:D11;", ") 'OS with ; list separator
=concatRange(A8:D11,", ") 'OS with , list separator or in a macro code
or
=concatRange(A8:D11)
See this blog post here: http://www.dullsharpness.com/2011/11/14/excel-vba-range-to-csv-range2csv-function/
You can use it like so, e.g. with a pipe delimiter:
=Range2Csv(A1:A255,"|")
Access your VBA editor using Alt+F11 and drop it into a module.
Code excerpt is here:
Option Explicit
'**********************************************
'* PURPOSE: Concatenates range contents into a
'* delimited text string
'*
'* FUNCTION SIGNATURE: Range2Csv(Range, String)
'*
'* PARAMETERS:
'* Range - the range of cells whose contents
'* will be included in the CSV result
'* String - delimiter used to separate values
'* (Optional, defaults to a comma)
'*
'* AUTHOR: www.dullsharpness.com
'*
'* NOTES: [add'l notes removed for brevity]
'*
'**********************************************
Public Function Range2Csv(inputRange As Range, Optional delimiter As String)
Dim concattedList As String 'holder for the concatted CSVs
Dim rangeCell As Range 'holder cell used in For-Each loop
Dim rangeText As String 'holder for rangeCell's text
'default to a comma delimiter if none is provided
If delimiter = "" Then delimiter = ","
concattedList = "" 'start with an empty string
'Loop through each cell in the range to append valid contents
For Each rangeCell In inputRange.Cells
rangeText = rangeCell.Value 'capture the working value
'Only operate on non-blank cells (i.e. Length > 0)
If Len(rangeText) > 0 Then
'Strip any delimiters contained w/in the value itself
rangeText = WorksheetFunction.Substitute(rangeText, delimiter, "")
If (Len(concattedList) > 0) Then
'prepend a delimiter to the new value if we
'already have some list items
concattedList = concattedList + delimiter + rangeText
Else
'else if the list is blank so far,
'just set the first value
concattedList = rangeText
End If
End If
Next rangeCell
'Set the return value
Range2Csv = concattedList
End Function
concatenate(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, a21, a22, a23, a24, a25, a26, a27, a28, a29, a30, a31, a32, a33, a34, a35, a36, a37, a38, a39, a40, a41, a42, a43, a44, a45, a46, a47, a48, a49, a50, a51, a52, a53, a54, a55, a56, a57, a58, a59, a60, a61, a62, a63, a64, a65, a66, a67, a68, a69, a70, a71, a72, a73, a74, a75, a76, a77, a78, a79, a80, a81, a82, a83, a84, a85, a86, a87, a88, a89, a90, a91, a92, a93, a94, a95, a96, a97, a98, a99, a100, a101, a102, a103, a104, a105, a106, a107, a108, a109, a110, a111, a112, a113, a114, a115, a116, a117, a118, a119, a120, a121, a122, a123, a124, a125, a126, a127, a128, a129, a130, a131, a132, a133, a134, a135, a136, a137, a138, a139, a140, a141, a142, a143, a144, a145, a146, a147, a148, a149, a150, a151, a152, a153, a154, a155, a156, a157, a158, a159, a160, a161, a162, a163, a164, a165, a166, a167, a168, a169, a170, a171, a172, a173, a174, a175, a176, a177, a178, a179, a180, a181, a182, a183, a184, a185, a186, a187, a188, a189, a190, a191, a192, a193, a194, a195, a196, a197, a198, a199, a200, a201, a202, a203, a204, a205, a206, a207, a208, a209, a210, a211, a212, a213, a214, a215, a216, a217, a218, a219, a220, a221, a222, a223, a224, a225, a226, a227, a228, a229, a230, a231, a232, a233, a234, a235, a236, a237, a238, a239, a240, a241, a242, a243, a244, a245, a246, a247, a248, a249, a250, a251, a252, a253, a254, a255)
PowerShell it!
"concatenate(a$((1..255) -join ', a'))" | clip
Open the text file copy and paste
"To quickly select cells you can press CTRL and click on cells you want to be included in the concatenate function.
Example,
Select a cell
Type =concatenate( in formula bar
Press and hold CTRL button and click cells to be included.
Release CTRL button
Type ) in formula bar and press Enter"
This VBA function will concatenate the contents of cells, with an optional delimiter, if needed. Copy it into a standard module:
Option Explicit
Function Concat(CellRange As Range, Optional Delimiter As String) As String
' this function will concatenate a range of cells and return the result as a single string
' useful when you have a large range of cells that you need to concatenate
' source: http://chandoo.org/wp/2008/05/28/how-to-add-a-range-of-cells-in-excel-concat/
Dim retVal As String, dlm As String, cell As Range
retVal = ""
If Delimiter = Null Then
dlm = ""
Else
dlm = Delimiter
End If
For Each cell In CellRange
If CStr(cell.Value) <> "" And CStr(cell.Value) <> " " Then
retVal = retVal & CStr(cell.Value) & dlm
End If
Next
If dlm <> "" Then
retVal = Left(retVal, Len(retVal) - Len(dlm))
End If
Concat = retVal
End Function
If your looking for a pure Excel approach (ie no VBA) then the method proposed by James Jenkins is best.
If you are happy using VBA then open up the VBA editor, add a new module, and add this code:
Option Explicit
Public Function JoinText(cells As Variant,Optional delim_str As String) As String
If cells.Columns.count < cells.Rows.count Then
JoinText = Join(WorksheetFunction.Transpose(cells), delim_str)
Else
JoinText = Join(WorksheetFunction.Transpose(WorksheetFunction.Transpose(cells)), delim_str)
End If
End Function
To open the VBA editor easily press Alt-F11.
To insert a module you right-click on the workbook listed in the 'Project' window.
The function is called from excel as follows:
=JoinText(A1:C1)
If you want to add a delimiter (eg comma):
=JoinText(A1:C1,",")
The purpose of using the transpose function is to turn the 2d array, 'cells', into a 1d array. The reson for this is that the VBA function Join only accepts a 1d array.
The reason for using two of them is if JoinText is looking at a row of cells (which is still just a 2d array) then the first call to transpose, transposes this 2d row array into a 2d column array, the second call turns it into a 1d array.
My preferred method is to cut-and-paste the values into an editor that allows regular expressions, then I simply remove the tabs (or spaces) with a find and replace on my current selection.
You can also use this to insert commas, whitespace, or whatever you want.
It's a ton faster than typing =concatenate(A1,",","A2",",",......)
It isn't purely Excel, but there is an easy way to do this with Word.
Select the cells you want to concatenate and copy/paste them into Word. This creates a table.
Select the entire table.
Convert the table to text. Use paragraph marks (or something else that does not appear in your text) as separators.
Select all of the text.
Use Replace to remove the paragraph marks. (In the "Find what" box, enter ^p. Leave the "Replace with" box empty.)
If you have Excel 2016, you can use an array formula:
Enter
=concat(a1:a255)
into the cell, then press
[ctrl]+[shift]+[enter]
where the values that you would like to concantenate start in row 2 column 3 of your sheet
Sub GOWN()
roww = 2
Do While cells(roww, 2) <> ""
aa = cells(roww, 3)
dd = dd & aa & ","
roww = roww + 1
Loop
cells(roww + 1, 3) = dd
End Sub
Shamelessly copied from this site:
Select the cell where you need the result.
Go to formula bar and enter ... "=A1:A5"
Select the entire formula and press F9 (this converts the formula into values).
Remove the curly brackets from both ends.
Add =CONCATENATE( to the beginning of the text and end it with a round bracket).
Press Enter.
What is particularly revelatory here is that when editing a formula, pressing F9 replaces the formula with the result of that formula. Where that's a range, it replaces it with a list of the contents of that range.
Just add your deliminator in one concatenation:
=concatenate(A1, ",")
Then copy all the concatenations, paste them as Values. Then Copy those Values, paste them in a transposition. Then copy the Transposed values and paste them into a word editor. Do a find for the deliminator AND the space preceding the values and do a replace for JUST the deliminator. This should give you a concatenated string of all the values with a deliminator. This is much easier than other options.

Excel Macro- remove a word that starts with certain characters

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
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
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
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)
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.

Resources