Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-21-2015, 11:11 AM
rsrasc rsrasc is offline Sorting Numbers in a Document Windows 7 64bit Sorting Numbers in a Document Office 2010 64bit
Competent Performer
Sorting Numbers in a Document
 
Join Date: Mar 2014
Location: Germany
Posts: 148
rsrasc is on a distinguished road
Default 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!
Reply With Quote
  #2  
Old 01-04-2016, 03:21 PM
rsrasc rsrasc is offline Sorting Numbers in a Document Windows 7 64bit Sorting Numbers in a Document Office 2010 64bit
Competent Performer
Sorting Numbers in a Document
 
Join Date: Mar 2014
Location: Germany
Posts: 148
rsrasc is on a distinguished road
Default

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
Reply With Quote
  #3  
Old 01-04-2016, 06:14 PM
macropod's Avatar
macropod macropod is offline Sorting Numbers in a Document Windows 7 64bit Sorting Numbers in a Document Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
after:
.Sort
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 01-04-2016, 11:47 PM
rsrasc rsrasc is offline Sorting Numbers in a Document Windows 7 64bit Sorting Numbers in a Document Office 2010 64bit
Competent Performer
Sorting Numbers in a Document
 
Join Date: Mar 2014
Location: Germany
Posts: 148
rsrasc is on a distinguished road
Default

Hi Paul,

Thank you for the "delete duplicate rows" code.

It does the job!!!

Thank you so much for your support.

Cheers!
Reply With Quote
Reply

Thread Tools
Display Modes


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 Numbers in a Document 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 Numbers in a Document Sorting paragraph numbers CommDude Excel 1 01-02-2011 04:38 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:19 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