Search for specific word in VBA string - vba

There is a code I am using to search for specific words in a VBA string. Currently I'm using InSTR:
tag1 = InStr(1, dish, keyword, vbTextCompare)
However the challenge is that the search is comparing strings and not words.
E.g. If I'm searching for "egg" as a keyword in a string - Eggplant Pizza, it's returning a true value.
Ideally I would simply like to search if the word "egg" exists in a string. Is there a better function I can use?

You could also use Regular Expressions to achieve this in VBA.
Looking specifically at the ^ and $ operators to force the full word match.
So in your case something like ^Egg$ as the pattern should do what you want.
See here for some good help on this:
How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops

InStr is the way to go. For finding out how many times a string is present in a text, you can use this one-line code. Here I use your example.
Debug.Print UBound(Split("Eggplant Pizza", "Egg"))
To make the code case insensitive, you can put Option Compare Text on top of your code module.

Related

Does regex not work in Excel search?

I am trying to search for trailing whitespaces in text cells in Excel. Knowing that Excel search accepts regex, I expected to leverage on the full feature set, but was surprised to find that some features do not seem to work.
For example, I have some cells with strings like ELUFA\s\s\s\s\s (note: in my excel sheet there is no \s, but just blank invisible whitespaces, after ELUFA, but I had to add these \s in here otherwise Stackoverflow would just remove these whitespaces and the string would just appear to be ELUFA) or NATION CONFEC.\s with trailing whitespaces.
I used the expression [A-Z.]{1}\s+$ into the excel search function expecting that it would return search results for these cells, but it does not, and just tells me that nothing is found.
However, what I find really funny is that Excel search is somehow able to interpret a regex like this A *. Using this expression, excel search does find for me only the ELUFA\s\s\s\s\s cells, and no other cells which do not match this regex.
Is there some kind of limitations as to what subset of the full REGEX that Excel search accepts? How do we get excel search to accept the full REGEX feature set as described here?
Thank you.
The Excel SEARCH() function does not support full regex. It actually only supports two wildcards, ? and *. From the documentation:
You can use the wildcard characters — the question mark (?) and asterisk (*) — in the find_text argument. A question mark matches any single character; an asterisk matches any sequence of characters. If you want to find an actual question mark or asterisk, type a tilde (~) before the character.
If you want to match spaces then you will have to enter them as literals. Note that finding any amount of trailing spaces could be as simple as ELUFA\s, with one space at the end, because that would actually match one, or more than one, space.

Standard Excel function or write my own for the following string manipulation

I have about 200 strings that I need to manipulate to make my SQL querying easier. Suppose I have a string like
OR this.quantity LIKE 'transient' OR this.quantity LIKE 'gradient'
I would like to add wildcards to the beginning and end of 'transient' and 'gradient' as such:
'%transient%', '%gradient%'
Suppose further that I have about 198 more of these (unique of course). Is there a function I can use for this manipulation or do I need to write my own VBA code to accomplish this?
You are not using wildcards, so in SQL, you would just use in:
this.quantity IN ('transient', 'gradient')
If you can't edit/replace, you'd have to get into text parsing formulas. With your example above in A2, the formula to replace the first two quotes would be
=LEFT(A2,SEARCH("'",A2))&"%" &MID(A2,SEARCH("'",A2)+1,SEARCH("'",A2,SEARCH("'",A2)+1)-SEARCH("'",A2))&"%"
Yes its ugly and I didn't write it from scratch. Use the search function to find the ' Then put the text in. The second ' is searching the same string, but using the place of the FIRST ' as the starting point; +1. Not sure if it's the BEST way, but I've used Excel to do many text manipulations in the past.

How do I find specific substring in a string

My full question title was too long, but it should be asked here:
How do I find all instances of a specific substring in a string accounting for spaces and special characters potentially being on either side of the substring
What I mean is this. I am writing a SQL code formatting assistance program in VB.Net. This program will help when I am following up on truly porrly writen SQL. A for instance is (and please ignore the syntax failure here, I am not good at writting bad code in SQL):
if exists(
select *
from dbo.table
where field1 = (if exists (select field1
from dbo.table1
where field2 = '123')
select field1 from table2)
My program is still in the early stages. I have already identified most of the keywords, and written the code that will put them in the proper case format. So in the bad code example from above all of the selects will be Select. To do this I have created a list of key words in array form, and use this array in the following function:
Private Function FindAndReplace(ByVal findWhat As String, _
ByVal replaceWith As String, ByVal focusLine As String) As String
focusLine = Microsoft.VisualBasic.Strings.Replace(focusLine, findWhat, _
replaceWith, 1, -1, Constants.vbTextCompare)
Return focusLine
End Function
The good news is this works really well with words like Select. Words like If, Go, On, and End are a bit more challenging. If I have the word Send, it will replace it with the word SEnd because End is a keyword. On many of these instances I can account for this by putting the smaller words before the larger words. I have added Send as a keyword because of the number of times that word appears in user messages on our systems.
I cannot seem to account for words like On, If, or Go. I considered searching for " Go ", " On ", ")Go ", " On(", etc. but there are times when Go is going to be the first word on the line...or the only.
What I need is a VB.Net means of searching a string for all of the instances of a given substring (such as If). I was thinking I would check if it was the first word in the string, or seeing if it is surrounded by any combination of spaces or special characters (or not surrounded by other letters and underscores, etc.). I would update those that met my requirements, and leave the others alone.
I am drawing a blank on how to do this, and I could really use some assistance.
I am writing a SQL code formatting assistance program
I'd recommend starting with an existing SQL parser.
Pete Sestoft's excellent Programming Language Concepts book introduces parsing fundamentals including writing Lexer and Parser specifications for Micro-SQL in Chapter 3.
The open source Irony project includes an SQL grammar sample.
Use your favourite search engine to find others.
What I need is a VB.Net means of searching a string for all of the instances of a given substring
There are a number of ways of achieving this:
Split the string into words and then search those words for instances.
Use a state machine to iterate over the string and check words after white space.
With option 2 you can handle quoted strings and maintain an index for each word, here's a short example in F#: http://fssnip.net/f6

VBA Macro to extract strings from word doc

i have a word document containing several strings. These strings have the first part always the same, for example ABC_001, ABC_002, ABC_003. I need to search for "ABC_" substring in the doc, extract all the occurences ("ABC_001", "ABC_002", "ABC_003") and copy them in an Excel sheet.
Anyone can help?
Thanks in advance.
You can reference the VBScript Regular Expressions 5.5 and regex them.
Have a look at http://www.macrostash.com/2011/10/08/simple-regular-expression-tutorial-for-excel-vba/
and http://txt2re.com/
and some of VBA multiple matches within one string using regular expressions execute method
EDIT:
Actually it is probably easier to go to data and "Get external data" choose de-limiter and import, either manually or record a macro to get a feeling for the vba structure.
This should get you all the entrys in seperate cells, then go over them with a MID to get the part you need

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