Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-11-2015, 08:17 PM
equalizer88 equalizer88 is offline wildcard search and add (not replace) sequential numbers in front Windows 7 32bit wildcard search and add (not replace) sequential numbers in front Office 2010 32bit
Novice
wildcard search and add (not replace) sequential numbers in front
 
Join Date: Jul 2015
Posts: 15
equalizer88 is on a distinguished road
Default wildcard search and add (not replace) sequential numbers in front

My data is 1-3 lines unique code that is first element in data segment. May Not be new paragraph. First field is 1-5 digits comma and then 1 digit and comma

Original File
Line 1 123,2,text1, text2,....
Line 2 ....text 10
Line 3 145,3,text1....text 5


Line 4 156, 2, text 1...text5
Line 5 ......................text10
Line 6 ......................text15
...........
Line 7000 4160,3,text1......text4


Desired Output - Search for ([0-9],[0-9]) and insert (001) before search term and (002) before next search term

Output File
Line 1 (001) 123,2,text1, text2,....
Line 2 ....text 10
Line 3 (002) 145,3,text1....text 5
Line 4 (003) 156, 2, text 1...text5
Line 5 ......................text10
Line 6 ......................text15
Line 7 (004) 160,3,text1......text4


Maybe 1000 pages

I used https://www.msofficeforums.com/word-...ight-text.html as template and have just seach working.


Code:
Sub Wildcard_Search_Insert_Nums()
Application.ScreenUpdating = False
Options.DefaultHighlightColorIndex = wdYellow
With ActiveDocument.Content.Find
  .ClearFormatting
  .Text = "([0-9],[0-9])"
  With .Replacement
    .Text = "TEST^&XXX"
    .ClearFormatting
    .Highlight = True
  End With
  .Forward = True
  .Wrap = wdFindContinue
  .Format = True
  .MatchWildcards = True
  .Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
Need to an alternate method for search/replace with a loop line by line(?)
with n=n+1 inside loop?

Really frustrated. Would appreciate any help. Obviously can use C program to do this but want functionality for all users, not 3 machines out of hundreds that have compilers.
Reply With Quote
  #2  
Old 07-11-2015, 09:25 PM
gmayor's Avatar
gmayor gmayor is offline wildcard search and add (not replace) sequential numbers in front Windows 7 64bit wildcard search and add (not replace) sequential numbers in front Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

With your example, which is not consistently formatted, e.g. Line 4 format has an extra space compared with Line 3, the following should do the job

Code:
Dim oRng As Range
Dim i As Long
    Set oRng = ActiveDocument.Range
    i = 1
    With oRng.Find
        Do While .Execute(FindText:="[0-9]{1,5},[ 0-9]{1,2},", MatchWildcards:=True)
            oRng.Text = "(" & Format(i, "000") & ") " & oRng.Text
            oRng.Collapse 0
            i = i + 1
        Loop
    End With
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 07-13-2015, 06:30 AM
equalizer88 equalizer88 is offline wildcard search and add (not replace) sequential numbers in front Windows 7 32bit wildcard search and add (not replace) sequential numbers in front Office 2010 32bit
Novice
wildcard search and add (not replace) sequential numbers in front
 
Join Date: Jul 2015
Posts: 15
equalizer88 is on a distinguished road
Default

Thanks so much! Can you explain the oRng.Collapse 0 command? The concept of the .Collapse command is not clear to me, I understand the collapsestart and collapseend (don't like the collapse name), but why anything else?

What is the difference between the two search methods above? The first is concurrent find and replace and the second is one find at a time? Is that why first method is the preferred method?

I'll have to get Mastering VBA for Office book to learn the concepts unless you have a better suggestion?
Reply With Quote
  #4  
Old 07-14-2015, 01:36 AM
gmayor's Avatar
gmayor gmayor is offline wildcard search and add (not replace) sequential numbers in front Windows 7 64bit wildcard search and add (not replace) sequential numbers in front Office 2010 32bit
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

oRng.Collapse 0 is the numeric equivalent of oRng.Collapse Direction:=wdCollapseEnd
(1 would be the equivalent of wdCollapseStart).

Collapse is used here to allow the search to restart from the end of the current found text range.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 05-07-2016, 08:04 PM
equalizer88 equalizer88 is offline wildcard search and add (not replace) sequential numbers in front Windows 7 32bit wildcard search and add (not replace) sequential numbers in front Office 2010 32bit
Novice
wildcard search and add (not replace) sequential numbers in front
 
Join Date: Jul 2015
Posts: 15
equalizer88 is on a distinguished road
Default

This is solved!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Issue with wildcard search mysterytramp Word 0 05-13-2015 10:40 AM
search and replace a field containing text and numbers H28Sailor Word 6 09-11-2014 01:00 AM
Issue with Wildcard Search and Replace linusthedog Word 3 09-04-2014 03:32 PM
Where is the error in my wildcard search? Ulodesk Word 10 06-30-2014 01:46 PM
Wildcard search help. Kempston Word 0 11-13-2009 03:58 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 01:33 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft