Find and delete text within a cell in excel - vba

I have pasted text in cell A1 that may look like this:
"Vanguard - Total Market - Composite" or "Vanguard - Total Market - Commingled"
I want to cut off the " - Composite" or " - Commingled" and return the rest in cell B1. Currently I'm using this formula in B1:
=LEFT(A1,FIND(" - Composite",A1)-1)
However, I can't figure out how to look for multiple terms (i.e. Composite or Commingled). Is there a formula that could accomplish this? If not, how could I do it in VBA?
Thanks for the help!

If I understand correctly, you're simply looking to strip everything past the second occurrence of -, i.e. returning the trimmed (extra whitespace removed) text left of the first - character. Adapting this solution to locate the last word in the string, this would be it:
=TRIM(SUBSTITUTE(LEFT(A1,LEN(A1)-LEN(MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"-",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,"-",""))))+1,LEN(A1)))),"-","",2))
This formula will work with or without the spaces around the -, and regardless of what follows:
If even spacing is important, you can wrap it with SUBSTITUTE functions:
=SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(LEFT(A1,LEN(A1)-LEN(MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"-",CHAR(1),LEN(A1)-LEN(SUBSTITUTE(A1,"-",""))))+1,LEN(A1)))),"-","",2)),"-"," - ")," "," ")
And now you have clean, identically-formatted output for all cases:

I've made this formula that will work with many possibilities as long you fill the possibilities range, not needing to change the formula when there's a new one. Here it is:
=LEFT(A2,FIND(INDIRECT(ADDRESS(SUMPRODUCT((--ISNUMBER(SEARCH(Possibilities,A2)))*ROW(Possibilities)),SUMPRODUCT((--ISNUMBER(SEARCH(Possibilities,A2)))*COLUMN(Possibilities)))),A2)-1)

You can use the SUBSTITUTE function and chain multiple calls:
=SUBSTITUTE(SUBSTITUTE(A1," - Composite", ""), " - Commingled", "")

Related

How to get last word in a cell using Excel Formula

I am trying to get the last word in a cell however, it does not seem to be appearing getting the last word of the cell. It seems to get the partial match and not working correctly. I am using =IFERROR(RIGHT(AP2,SEARCH(" ",AP2)-1),AP2). This works great for getting the first word in a cell even where the first word is the only word in a cell. Any ideas on why this is not getting the last word correctly?
You may try this UDF which can be used in another sub routine or on the worksheet as well.
Function GetLastWord(ByVal Str As String) As String
Dim arrStr
arrStr = Split(Str, " ")
GetLastWord = arrStr(UBound(arrStr))
End Function
If you need to use it on the worksheet, assuming your string is in A1 then try this...
=GetLastWord(A1)
Your formula is based on the first occurrence of a space in the string.
For example with "Man City V Stoke" in the formula SEARCH(" ",AP11) is looking for the first occurrence of a space which occurs at position 4. You are then subtracting 1 from this and using this as the number of characters to return from the right of the original string. So you get Right("Man City V Stoke",3) which is "oke".
A better way is a formula such as
=TRIM(RIGHT(SUBSTITUTE(AP11," ",REPT(" ",LEN(AP11))),LEN(AP11)))
find text after last space
The part written: =SUBSTITUTE(AP1," ",REPT(" ",LEN(AP1)))
This inserts spaces the length of the string (16) in place of every occurrence of a space i.e.
Man City V Stoke
When you then do RIGHT(SUBSTITUTE(AP11," ",REPT(" ",LEN(AP11))),LEN(AP11))
you are guaranteed to only get whitespace followed by the last word. You then use TRIM to get rid of this white space.
As far as I can tell the below worked for me.
=TRIM(RIGHT(SUBSTITUTE(AP40," ",REPT(" ",100)),100))
I can only assume my other method is unreliable at getting the last word.

Search for a first characters in a cell until a "-"

I am trying to use a find function. However I need to only use the first part of the contents of the cell, before a "-" (hyphen) . (i.e. yens2856826,28hn-72keysto) writing this example I want to just get the "yens2856826,28hn". And use that for my find function.
Thanks in advance!
Use the Left Function to cut the Part out and InStr to search for chars in a string.
MsgBox Left("yens2856826,28hn-72keysto", Instr("yens2856826,28hn-72keysto", "-") -1)

Excel using Replace on Formulas

I have a bunch of formulas all over different rows and columns in my Excel sheet. Some of the formulas are of the form: =($B$12-G12)*$B$3*G3
All I want to do is replace the ")" with ")^2". I tried using the Ctrl + F -> Replace section to look in Formulas but for some reason Excel says cannot find a match...All the cells I am trying to replace are of the form:
=(Cell1-Cell2)*Cell 3*Cell4
I just want to replace that )* by ^2*
What can I do?
This is what I have tried searching:
Make sure the Look in option is set to Formulas:
EDIT#1:
As per your comment, you must use tilda in the Find string:
Find and search using:
)~*
What I was having difficulty with was the * and I found the solution is to just use a ~ and it will fix the problem.
In summary of the article I left as a comment (http://www.mrexcel.com/archive/General/29013.html), you need to use ~ [the character to the left of the 1 on a standard English QWERTY keyboard] before *, in order to specifically tell excel that you want to look for an asterisk, not use the asterisk as a wildcard.

VBA script in excel to find and highlight text

I am looking for a VBA script that will help me find certain keywords in a cell and if found highlight the entire row. Below are my requirements:
I have a database of words eg hell, get out, shut up, don't you dare etc. I need a macro to search the data in column "E" of excel and in case any of the cell in column "E" contains any word listed in the database (irrespective of the case of the word upper or lower)the entire row is highlighted. The word can be in the beginning, middle or end of the cell and the macro should be able to find that word and highlight the column.
Seeking help from all VBA masters for this.
You can do this with conditional formatting, instead of VBA.
Conditional formatting works by applying a 'second formula' to a given cell. If the 'second formula' results in TRUE, then special formatting conditions can be applied.
EXAMPLE CONDITIONAL FORMATTING
For example, if you have a single column of Data, A:A, and you want to check if that column has the exact string "hello world", you could add a conditional format [Home ribbon, Styles section, Conditional Formatting] that turns a cell yellow with this formula:
=$A1="hello world"
This will only result in TRUE if the cell in column A at that row equals exactly "hello world" [note that Column A has an absolute-reference $, and row 1 does not, so row 1 is relative to the position of the cell in the condiitonal format rule].
To check to see if any row in column A includes hellow world, we need to add a SEARCH function, which checks to see if a small search string is inside of a larger string:
=SEARCH("hello world",$A1)>0
Because SEARCH by default returns the first character in a larger string that matches the search term (and if it finds nothing, it returns #N/A), we check to see if our search for "hello world" in column A returns a number.
SEARCHING MULTIPLE COLUMNS
Now, to see if ANY column, say from A-D, includes "hello world", we concatenate each value of each column so that it gives us a single string, which we can search through for "hello world", like so:
=SEARCH("hello world",$A1&$B1&$C1&$D1)>0
This will first create a single string, equal to A1 & B1 & C1 & D1 all in a row. Then it will search that newly created string to see if "hello world" is inside it, and return a number value if it is.
ARRAY FORMULA BASICS
Finally, we need to do the tricky part - searching for multiple terms instead of just "hello world". This is called an Array Formula. An array formula works by performing a single operation on multiple cells, and then returning multiple results in an Array. In an Excel sheet, an array formula must be confirmed with CTRL + SHIFT + ENTER (instead of just ENTER), but in conditional formatting, you actually don't need to do anything special - it will recognize an array formula without a special command.
As an example of conditional formatting, see this example, which checks whether any value from A1:A5 = 10, and if it does, it gives us the value in B1:B5:
=IF(A1:A5=10,B1:B5,"")
Remember in Excel on a worksheet, this would be confirmed by pressing CTRL + SHIFT + ENTER. If you do test this, it will give you the following result, assuming A2 = 10 and A5 = 10:
={"";B2;"";"";B5}
This result would actually be hidden, because Excel can't "collapse" an array function on its own. So assume column B had values, and we actually want to sum them together. We would then wrap the Array formula in a SUM function:
=SUM(IF(A1:A5=10,B1:B5,""))
As you can see if you test this, we have actually created our own SUMIF function, using Array formulas instead of the built-in SUMIF.
SEARCHING FOR MULTIPLE TERMS WITH ARRAY FORMULAS
So now we apply these principles to the conditional formatting, to create an array formula which will check our concatenated 'NEW STRING' for any number of provided terms, as follow [Assumes the search terms are typed into cells E1:E10]:
=SUM(SEARCH($E$1:$E$10,$A1&$B1&$C1&$D1)>0)
This formula can be placed as a conditional formatting rule which reaches all of A:D. Set the rule to highlight / change format in whatever way you like.

Custom number formatting to display 0000.00

I have a database with all the U.S. tariff codes that I need to format.
The numbers appear as:
010129
Whereas I need them to appear as:
0101.29
I have tried to use custom formatting to match the decimals with ####.?? but it does not work.
The database contains 19,423 rows of data; therefore, if there is a better way to format the numbers using VBA, I will gladly take that response as well. Thank you!
Because you have Text rather than Numbers, reformatting is not enough. With your data in A1, in B1 enter:
=LEFT(A1,5) & "." & MID(A1,6,3)
and copy down. For example:
EDIT#1
If the double-quotes do not appear in the data then use:
=LEFT(A1,4) & "." & MID(A1,5,2)
See: