|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Code to find numerical string + space throughout document & replace them with Comma
I am new to Word Macros. I am assisting a local historical society by scanning old electoral rolls for the area, performing OCR and then copying the data into excel tables so the data can be searched electronically.
The OCR process produces a .txt file which I then open in Word 16 so I can clean it up and comma delineate the fields of data. The entries are: Roll Number (space) Surname, Forenames, Address, electoral district,gender followed by a carriage return (paragraph mark). My problem is to incorporate a sub routine (maybe not the correct technical term) to place a comma after the Roll Number which is a number between 1 and 99,999. I have tried to use the find and replace function, (which I use to get rid of unwanted spaces and errors when the OCR program identifies . (periods) as commas. I note the street address usually contains numbers as does the Electoral District. Any assistance would be appreciated in formulating the appropriate code. |
#2
|
||||
|
||||
Try a wildcard Find/Replace, where:
Find = ([^13^l,][0-9]{1,})^32 Replace = \1,^32
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Code to find Numerical string etc
Thanks for your reply Paul. I am afraid your code doesn't work. As I understand it:
The "find" expression translates ^13 is code for the paragraph break - I don't understand the expression or function ^| - or is that a lower case L?. I assume the [0-9]{1,} is to find a numerical string between zero and nine. What is the function of the numeral one in curly brackets? I gather the expression ^32 represents a space. My understanding of the "replace" function is that it replaces the first element in the find expression (i.e. the \1) with a space. I don't want a space, I need a comma after the number represented by the numerical string. The numerical string can be any number (i.e. set of numerals comprising a number, between 1 and 99999. The numbers above 1000 are not comma delineated. The following text is an example of the original document: 641 Beard, Ernest Noel, 31 Tennyson st., S.8, analyst, M 642 Beard, Suzanne Louise, 31 Tennyson st., S.8, sales; F 643 Beasley, Howitt, 72 Beaumaris pde., S.21, foreman, M 644 Beasley, Joyce Ivy, 18 Victory st., S.8, mechanic, F 645 Beaton, Flora Janet, 44 Sims st., S.8, home duties, F I want the result of the macro to produce 641,Beard, Ernest Noel, 31 Tennyson st., S.8,,analyst, M 642,Beard, Suzanne Louise, 31 Tennyson st., S.8, sales; F 643,Beasley, Howitt, 72 Beaumaris pde., S.21, foreman, M 644,Beasley, Joyce Ivy, 18 Victory st., S.8, mechanic, F 645,Beaton, Flora Janet, 44 Sims st., S.8, home duties, F Your advice would be appreciated |
#4
|
||||
|
||||
The wildcard Find/Replace works for me with your sample data. Did you check the 'use wildcards' option?
FWIW the Find expression finds any of a paragraph break, line break or comma followed by a number then a space. The Replace expression simply inserts a comma after the number.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Paul,
Once again thank you for your prompt response. You are correct, I did not activate the wildcards option. Your code actually created a space after the comma but I resolved this by deleting the following ^32. I "roughly" follow the code generated in the macro but would appreciate any pointers to a programming guide that explains the process in a little more detail. That is onr that does not cost an arm and a leg! Many thanks for your prompt and most helpful assistance. Cheers Robert Kay |
#6
|
||||
|
||||
The F/R expression I posted would not add a space - it will only preserve whatever space(s) were already there. It's also not a macro; it's just a wildcard Find/Replace.
For an outline of Word's wildcard Find/Replace expressions, see: https://support.office.com/en-us/art...4-7708c6c779b7, especially the discussions concerning wildcards and regular expressions. See also: https://wordmvp.com/FAQs/General/UsingWildcards.htm
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Code to find numerical string + space throughout document & replace them with Comma
Paul
Thank you for your response. I have recorded the Find and Replace routine as a macro. Thanks also for the suggested links - they are much appreciated. Cheers Robert Kay |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Find & Replace text in Field Code across multiple documents | RPM7 | Word VBA | 6 | 05-12-2017 12:58 AM |
Find and Replace with space | cheech1981 | Word | 3 | 03-29-2017 02:33 PM |
How to do multiple find and replace in string. | PRA007 | Word VBA | 2 | 01-06-2016 09:10 PM |
Find and replace a string of text | errtu | Word | 1 | 01-31-2013 02:09 PM |
Bad view when using Find and Find & Replace - Word places found string on top line | paulkaye | Word | 4 | 12-06-2011 11:05 PM |