#1
|
|||
|
|||
Sorting Numbers in a Document
Need help sorting (with a macro) the Reference Number information in a document with their associated text (represented by the "xxx").
Reference#5111.04 xxx xxx Reference#8111.03 xxx Xxx Xxx Reference#5111.02 xxx Xxx xxx xxx xxx xxx Reference#7111.05 xxx xxx xxx Reference#5112.99 xxx xxx Thanks! |
#2
|
|||
|
|||
Here is the code I use to sort my information. Still working on it (how to delete duplicate records) but it does the job.
Thanks Macropod for the code. Code:
Sub Demo() Application.ScreenUpdating = False Dim Rng As Range, i As Long With ActiveDocument With .Range Set Rng = .Duplicate .InsertBefore vbCr With .Find .ClearFormatting .Replacement.ClearFormatting .Forward = True .Format = False .MatchWildcards = True .Text = "^13Reference#[0-9]{4}" .Replacement.Text = "" .Wrap = wdFindStop .Execute End With Do While .Find.Found .Start = .Start + 1 Rng.Start = .End With Rng With .Find .Text = "^13Reference#[0-9]{4}" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute End With If .Find.Found Then Rng.Start = .Start Else Rng.Start = ActiveDocument.Range.End End If End With .End = Rng.Start .ConvertToTable NumColumns:=1, Format:=wdTableFormatNone, AutoFitBehavior:=wdAutoFitWindow .Duplicate.Cells.Merge .Collapse wdCollapseEnd .Find.Execute Loop End With While .Tables.Count > 1 .Tables(1).Range.Characters.Last.Next.Delete Wend With .Tables(1) .Sort .ConvertToText End With .Range.Characters.First.Delete .Range.Characters.First.Delete End With Application.ScreenUpdating = True End Sub Last edited by macropod; 01-04-2016 at 05:53 PM. Reason: Corrected code tags & formatting |
#3
|
||||
|
||||
To delete duplicate rows, you could insert:
Code:
For i = .Rows.Count To 2 Step -1 If .Rows(i).Range.Text = .Rows(i - 1).Range.Text Then .Rows(i).Delete Next .Sort
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
Hi Paul,
Thank you for the "delete duplicate rows" code. It does the job!!! Thank you so much for your support. Cheers! |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Sorting Index According to Page Numbers | mohsen.amiri | Word | 14 | 06-25-2015 09:50 PM |
Sorting a mail merge by a specific number in the document | redzan | Mail Merge | 4 | 05-17-2013 09:04 AM |
How do I refer to page numbers, when the numbers change as I prepare the document? | StevenD | Word | 5 | 11-29-2012 12:52 AM |
Words in my document were converted to numbers???? | MikeD23 | Word | 1 | 08-26-2012 11:09 AM |
Sorting paragraph numbers | CommDude | Excel | 1 | 01-02-2011 04:38 PM |