Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / August 2005

Tip: Looking for answers? Try searching our database.

finding numbers out of sequence in a document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cgusenetmail@hotmail.com - 02 Aug 2005 18:29 GMT
I am trying to write a macro that will look through a document and
verify that the numbers that are found are sequential.  All I need to
be able to do is verify that each 2 numbers are sequential.  The
document is simple plain text and the numbers all have markers around
them (i.e. [#.###]).  I have written something which works for 2
numbers at a time, but I don't know how to make it go throughout the
whole document and mark whenever 2 sets of numbers go out of sequence.
It needs to check every set of 2 numbers and if they are out of
sequence, highlight them or something, so it can be manually checked
easily.

here is my attempt so far (Word2003):

Sub extractnos()
'
'
'

Dim NosFindText As String ' number pattern to search for
Dim NosFoundText1 As String ' first number pattern string found
Dim NosFoundText2 As String ' second number pattern string found
Dim FirstNum As Double  ' first number found
Dim SecondNum As Double ' second number found
Dim NumLen1 ' the len for the first number pattern string
Dim NumLen2 ' the len for the second number pattern string

   x = 1

   NosFindText = "\[*\]"

' search for 2 numbers at a time to compare them
Do While x < 3
   With Selection.Find
       .Text = NosFindText
       .Forward = True
       .Wrap = wdFindContinue
       .MatchWildcards = True
   End With
   Selection.Find.Execute
   If x = 1 Then
       NosFoundText1 = Selection.Text
   End If
   x = x + 1
Loop

' extract the numbers from their strings
   If Selection.Find.Found = True Then
       NosFoundText2 = Selection.Text
       NumLen1 = Len(NosFoundText1)
       NumLen2 = Len(NosFoundText2)
       FirstNum = Mid(NosFoundText1, 2, NumLen1 - 2)
       SecondNum = Mid(NosFoundText2, 2, NumLen2 - 2)
       If FirstNum > SecondNum Then
           StatusBar = "Error!!!" ' if I find an out of sequence
number
       End If
   End If

End Sub
Anne Troy - 02 Aug 2005 20:54 GMT
Sorry. I'm not a programmer, but you could try a For Each until a real
programmer comes along. :)

*******************
~Anne Troy

www.OfficeArticles.com

> I am trying to write a macro that will look through a document and
> verify that the numbers that are found are sequential.  All I need to
[quoted text clipped - 55 lines]
>
> End Sub
Jean-Guy Marcil - 02 Aug 2005 22:38 GMT
cgusenetmail@hotmail.com was telling us:
cgusenetmail@hotmail.com nous racontait que :

> I am trying to write a macro that will look through a document and
> verify that the numbers that are found are sequential.  All I need to
[quoted text clipped - 6 lines]
> sequence, highlight them or something, so it can be manually checked
> easily.

Will this help?

'_______________________________________
Sub extractnos()

Dim NosFindText As String ' number pattern to search for
Dim NosFoundText As Range ' first number pattern string found
Dim FirstNum As Double  ' first number found
Dim SecondNum As Double ' second number found
Dim NumLen1  As Long ' the len for the first number pattern string
Dim NumLen2 As Long ' the len for the second number pattern string
Dim startRge As Range 'To save initial selection

NosFindText = "\[*\]"

Set startRge = Selection.Range
Application.ScreenUpdating = False

Selection.HomeKey wdStory

With Selection.Find
   .Text = NosFindText
   .Forward = True
   .Wrap = wdFindContinue
   .MatchWildcards = True
End With
Do While Selection.Find.Execute
   If NosFoundText Is Nothing Then 'First time around the range is not yet
set
       Set NosFoundText = Selection.Range
   Else
       NumLen1 = Len(NosFoundText.Text)
       NumLen2 = Len(Selection.Text)
       FirstNum = Mid(NosFoundText, 2, NumLen1 - 2)
       SecondNum = Mid(Selection.Text, 2, NumLen2 - 2)
       If FirstNum > SecondNum Then
           StatusBar = "Error!!!"
           NosFoundText.HighlightColorIndex = wdYellow
           Selection.Range.HighlightColorIndex = wdYellow
       End If
       Set NosFoundText = Selection.Range
   End If
Loop

startRge.Select

Application.ScreenRefresh
Application.ScreenUpdating = True

End Sub
'_______________________________________

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.