|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Searching for the lone string on a line that starts with a particular character
Given
;TOOL:5 - DIAM:.75 CRAD:0 REF GL:0 ;DESC: Q026 ;MATL: HSS M06 T5.5 (UAO,1) (UIO,Z(-1*L370)) (TCP,5) G00 X.429 Y-20.9056 Z11. A0. C0. S18000 M03 Z8.8065 G01 Z7.8065 F50. X-1.571 Y-20.3033 Z7.802 F80. A-.761 Y-19.7012 Z7.7895 A-1.525 C-3.776 Y-19.0993 Z7.769 A-2.288 Y-18.4977 Z7.7405 A-3.051 Y-17.8965 Z7.704 A-3.813 Y-17.296 Z7.6596 A-4.576 Y-16.6961 Z7.6072 A-5.339 Y-16.6045 Z7.5985 A-5.455 Y-16.0054 Z7.5368 A-6.218 Y-15.4072 Z7.4673 A-6.98 Y-15.151 Z7.435 A-7.307 Y-14.5543 Z7.3541 A-8.07 Y-13.9586 Z7.2653 A-8.833 Y-13.4262 Z7.1791 A-9.516 Y-13.3791 Z7.1711 A-9.576 Y-13.1104 Z7.1248 A-9.922 Y-12.7073 Z7.0523 A-10.44 Y-12.3041 Z6.9759 A-10.96 X-1.5754 Y-12.0936 Z6.9348 A-11.176 C-3.682 X-1.5888 Y-11.8832 Z6.8929 A-11.393 C-3.592 X-1.6112 Y-11.6734 Z6.8501 A-11.61 C-3.504 My current algorithm does a search for "X*Y", which in my implementation expects that nonspecific string to exist on one line. In this example, there is a initially single value for X (1.571), and only the other variables change. Eventually, it reverts back to changing X & Y values. I need to be able to search for the unchanging X or Y value and then make a decision on what to do once that is done. For the life of me, I cannot find the correct VBA syntax for finding a single string that starts with a particular letter (in this case "Y" and ends in a Paragraph Break. |
#2
|
|||
|
|||
It is more helpful to post an example document than paste text into your question. This enables others to see what is actually in your document, especially non-printing characters like paragraph and line breaks.
You should actually be searching for a line/paragraph break followed by Y, otherwise you’ll find every line that includes a Y. That can be done without wild cards. |
#3
|
|||
|
|||
Ok I will try that. Actually I meant a string that starts with "X" and ends with a paragraph break. I thought I had edited that yesterday. I have attached a snippet of relevant text. Anything above doesn't matter and what follows is hundreds of similarly configured lines. What I was trying to sole at the moment is how to construct a proper Find statement in VBA to find one single string by itself on a line (paragraph in Word, I guess) that starts with a particular character.
|
#4
|
|||
|
|||
By searching for a line/paragraph break followed by Y you’ll know that it is preceded by a line with only an X value.
|
#5
|
|||
|
|||
Finding a line/paragraph break followed by the character "Y" would only find you a string on a particular line that starts with "Y", but not necessarily the only word on that line? It seems to me it would return True for
Big Fish Yellow Dog and Big Fish Yankee Right? |
#6
|
|||
|
|||
Hi! It's not difficult:
Code:
Sub Test() Dim oRng As range Set oRng = selection.range With oRng.Find .ClearFormatting .Replacement.ClearFormatting .text = "Y[! ]@" & Chr(13) .Format = False .Forward = True .MatchWildcards = True .Wrap = wdFindStop .MatchAllWordForms = False .MatchSoundsLike = False If .Execute Then oRng.Select End With Set oRng = Nothing End Sub |
#7
|
|||
|
|||
Quote:
You have me at a disadvantage with the value of .Text. I've looked but nothing good really jumped out at me with regard to special characters. Microsoft Help was not much. What is a good reference for all such special codes when searching for text? I've seen combinations of [], (), /, \, *, whatever. I just want to be able to recognize when there is one, specific word/string alone on a line that starts with a letter of my choosing. In my case it will be an X or Y. No whitespaces in this string. |
#8
|
|||
|
|||
|
#9
|
|||
|
|||
Besides the gmayor's reference provided by Italophile my collection of bookmarks also has another gmayor's reference (I think, they are almost identical):
Replace using wildcards The following improved code allows finding different chrs/strings in the selected range using the inputbox: Code:
Sub Test() Dim oRng As range Dim myStr As String Set oRng = selection.range myStr = InputBox("Enter any number of case-sensitive chrs that start the word/string to find", "STRING") With oRng.Find .ClearFormatting .Replacement.ClearFormatting .text = myStr & "[! ]@" & Chr(13) .Format = False .Forward = True .MatchWildcards = True .Wrap = wdFindStop .MatchAllWordForms = False .MatchSoundsLike = False If .Execute Then oRng.Select End With Set oRng = Nothing End Sub |
#10
|
|||
|
|||
OK, bookmarked. Thanks! I'll play with these.
|
#11
|
|||
|
|||
You are welcome!
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Word macro for deleting a line that starts with a specific character + deleting the line before | eduardb | Word | 1 | 08-10-2022 03:17 AM |
How to move a line to another line that starts with a chain selected in the 1st one? | gloub | Word VBA | 24 | 03-19-2019 03:12 PM |
Searching for string on a formula | AMD2800 | Excel | 1 | 12-17-2014 10:41 AM |
Replace paragraph-marks (line-breaks) in tables with a character-string | Aztec | Word VBA | 2 | 04-02-2013 10:52 PM |
Finding or searching ^ character in word document | shahin3121 | Word | 2 | 03-05-2012 06:16 PM |