#1
|
|||
|
|||
Macro or Function to know wether a string is included in a text
Hello!!
I need to gather information from standard commercial reports, which include standard information, like "number of employees", or "issued capital". Not all the information is available in every commercial report, so I need a macro / function to search wether "number of employees" (for instance) is a string included in the current commercial report. Depending of the result I would run other instructions. Thanks in advance |
#2
|
||||
|
||||
Not knowing how exactly you are going to want to use this, I would suggest a standard find and replace string (you don't need to be replacing the text).
To get the code, record a new macro and go through the process of using find and replace to find your selected text. Once you have done that, edit the macro and add the other instructions that you want to run! |
#3
|
|||
|
|||
Quote:
Currently, my macro searchs the string and, if the string is found, execute the following instructions. The problem appears in case the string is not found, because the macro ALSO EXECUTE THE FOLLOWING INSTRUCTIONS. That`s because I need a way to make a difference between both cases (when the search is successfull and when it is not). I though about preparing a function or a variable to store the value TRUE / FALSE, depending on the result of the search (successfull or not successfull). My idea is to use such variable / function within an IF instruction, executing a different group of instructions depending on the success of the search. I don't know how to build such variable / function. That's my problem. Thanks any way |
#4
|
||||
|
||||
In the following example, I am running a Find and Replace string for the word 'dog', Then I am using If statements to choose what to do if I find the word, or not. (read the green tags for info on each section)
Code:
Sub Find_Then_IF_Macro() ' Example by Bird ' ' This first section is the Find string - the only really important ' line is the one in RED, as this is what we are searching for Selection.HomeKey Unit:=wdStory Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "dog" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With ' This next part is where we decide on the action to be taken if ' the text we are looking for IS present. ' I just chose to move the cursor 5 places to the right! If Selection.Find.Execute = True Then Selection.MoveRight Unit:=wdCharacter, Count:=5 ' This next section is what we want to happen if the text we are ' looking for is NOT found; in this case, I have stopped the code ' by using Exit Sub (i.e. STOP) Else If Selection.Find.Execute = False Then Exit Sub ' Now, to finish off, we have to add an End If statement for ' every If that we had. ' NOTE: If you had values other than 'Yes/No' or 'True/False' ' then you could have more Else, If statements. End If End If End Sub |
#5
|
|||
|
|||
Your solution works.
Thank you! It's my first question. I don't know how to do to add to your reputation in return. What should I do? |
#6
|
||||
|
||||
Just click on the scales - top right of any of my posts!
Thanks - glad to have helped! |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Help! for using an appropriate function/Macro in Excel | pawan2paw | Excel | 1 | 06-04-2009 12:28 PM |
Extract from String using Wildcard | whousedmy | Word | 0 | 05-21-2009 01:35 AM |
Templates: automatic text generation from Rich Text content control | Chickenmunga | Word | 0 | 10-01-2008 11:16 AM |
Need help on what function to use??? | Primeraman | Excel | 1 | 06-13-2006 10:16 AM |
Help with Combination function | sanasath | Excel | 0 | 12-13-2005 07:24 AM |