#1
|
|||
|
|||
Deleting lines above selected text
Hi All
I've been trying construct some VBA to help tidy up some text. As an example , I have lots of blocks of text like this : BACKSTREET BOYS SHAPE OF MY HEART NEW UK JIVE LONG DELETED ORIGINAL 2000 63321BX19 BACKSTREET BOYS QUIT PLAYING GAMES EX UK JIVE ISSUE 259807BX223 I'm trying to highlight all lines containg 'BX' , and then delete the line above each of them. It should then look like this : BACKSTREET BOYS SHAPE OF MY HEART 63321BX19 BACKSTREET BOYS QUIT PLAYING GAMES 259807BX223 Grrateful for any help Last edited by Chayes; 11-02-2013 at 06:59 PM. Reason: typo |
#2
|
|||
|
|||
One way (there are alternatives) would be:
Code:
Sub RemoveAboveBX() Dim oPara As Paragraph For Each oPara In ActiveDocument.Paragraphs If InStr(oPara.Range.Text, "BX") > 0 Then oPara.Previous.Range.Delete End If Next End Sub |
#3
|
|||
|
|||
Hi
Ok many thanks for that. It work perfecly first time. BTW Just out of interest what change would you make to the code to bring the second line up onto the end of the first? Perhaps with a dash so each would look like this: BACKSTREET BOYS - SHAPE OF MY HEART 63321BX19 BACKSTREET BOYS - QUIT PLAYING GAMES 259807BX223 Grateful for your time and expertise. Last edited by Chayes; 11-02-2013 at 09:48 PM. Reason: typo |
#4
|
|||
|
|||
Oh, and the above only really works if your "lines" are, in fact, paragraphs.
|
#5
|
||||
|
||||
You could use a wildcard Find/Replace, where:
Find = ([!^13]@)^13([!^13]@^13)[!^13]@^13([!^13]@BX[!^13]@^13) Replace = \1 - \2\3 No VBA required. If you really want VBA for this, simply record the above with the macro recorder.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
hi
Thanks for this. I had no joy , I'm afraid. i made a wildcard search using the search and replace codes you suggested , but it couldn't find any matches. My page looks like this : JON SMITH 37 ACACIA AVENUE HUYTON LIVERPOOL CHESHIRE NN9 6LP IGLESIAS JULIO TO ALL THE GIRLS I`VE LOVED 118743BX65 BACKSTREET BOYS MORE THAN THAT 27954BX236 Ideally after a search and replace it would look like this : JON SMITH 37 ACACIA AVENUE HUYTON LIVERPOOL CHESHIRE NN9 6LP IGLESIAS JULIO - TO ALL THE GIRLS I`VE LOVED 118743BX65 BACKSTREET BOYS - MORE THAN THAT 27954BX236 Grateful for any advice. |
#7
|
||||
|
||||
The Find/Replace expressions I posted are designed to process the kind of data referred to in your first post. What you have now posted is different. To process that data, you could change the wildcard Find/Replace expressions to:
Find = ([!^13]@)^13([!^13]@^13)([!^13]@BX[!^13]@^13) Replace = \1 - \2\3 or: Find = ([!^13]@)^13([!^13]@^13[!^13]@BX[!^13]@^13) Replace = \1 - \2
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
Hi
Ok thanks very much for this. It's working fine now , and I'm grateful your time and for your help. Best Wishes |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Macro to find text in between two characters and then format selected text? | qcom | Word | 5 | 02-19-2015 11:23 PM |
Deleting grid lines but keeping the axis lines | CoffeeNut | Excel | 0 | 04-01-2013 01:50 PM |
Importing selected lines from a txt file | Pedgonc | Excel | 2 | 01-09-2013 12:44 PM |
Deleting Lines Pulls Back Next Page | SQLUSA | Word | 5 | 12-18-2012 03:53 PM |
Deleting empty lines | lostsoul62 | Word | 5 | 04-16-2012 04:55 AM |