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

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)

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.

Find and delete text within a cell in excel

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", "")

Is it possible to use ASCII code in .MoveEndUntil?

I am new in VBA for Word
I was wondering if it is possible to use ASCII code in .MoveEndUntil for example: .MoveEndUntil cset:=Chr(13) & "-", Count:=wdForward
Yes, it is totally possible to use sample code from your question. However, it depends what you are trying to achieve. Your code will move the end of the selection until any of the specified characters are found in the document (see the documentation of the Range.MoveEndUntil method).
That means that moving the end of the range will stop as soon as a dash or a carriage return is reached.
However, this is probably not what you want. It looks like you are trying to extend the range until the next list item? If this should be the case then you can't use a simple Range.MoveEndUntil. You would have to expand the range to the end of the current paragraph and check whether the following paragraph has a list formatting.

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.

VB.Net string contains exact match

I'm wondering if there is a way to search for an exact text match in a text box
for example
using "if textbox1.text.contains("Hello") then" works
however i only want it to search for text "Hello" and if i have 2 words like this
HelloFriend
Hello Friend
I only want it to find the word matching so the second statement Hello Friend and not HelloFriend as this doesn't match the keyword.
Is this possible?
You can make a regular expression that matches the word with word boundaries:
if Regex.IsMatch(textbox1.Text, "\b" + Regex.Escape("Hello") + "\b") Then
try to checkout
this one
or
this one
may be this one will help you :)