Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Search for a number, copy it, exit the sub.

Dear Experts:

I would like to run a macro that performs the following tasks:

- Search for a number with the following make-up (##-###-##-##) in ALL STORY RANGES
- Only the CURRENT section (i.e. where the cursor currently resides) is to be searched
- As soon as the first occurrence of this string/number (##-###-##-##) is found, the number is to be copied and the macro is to exit.

Thank you very much in advance for your professional help.

Regards, Andreas
0
Andreas Hermle
Asked:
Andreas Hermle
  • 2
1 Solution
 
GrahamSkanRetiredCommented:
Hi Andreas,
I think that this should do it.
Sub FindNumber()
    Dim rng As Range
    Dim rng2 As Range
    Dim s As Integer
    
    s = Selection.Sections(1).Index
    ' Go through all story ranges in the document, including shapes,
    ' headers & footers.
    For Each rng In ActiveDocument.StoryRanges
        Set rng2 = rng 'set rng2 to first range in story
        Do
            If rng2.Sections(1).Index = s Then
                With rng2.Find
                    .MatchWildcards = True
                    .Text = "[0-9]{2}\-[0-9]{3}\-[0-9]{2}\-[0-9]{2}"
                    If .Execute() Then
                        rng2.Copy
                        Exit Sub
                    End If
                End With
            End If
            Set rng2 = rng2.NextStoryRange
        Loop Until rng2 Is Nothing
    Next rng

End Sub

Open in new window

0
 
royhsiaoCommented:
To select only the CURRENT section (i.e. where the cursor currently resides)  and search the (##-###-##-##).

You could do the following:
Sub Currnet_Paragraph_Find_Copy()
Dim r As Range
Set r = ActiveDocument.Range(Start:=0, _
End:=Selection.Start)
Dim i As Integer
i = r.ComputeStatistics(wdStatisticParagraphs) + 1

ActiveDocument.Paragraphs(i).Range.Select
     With Selection.Find
        .Text = "^#^#" & "-" & "^#^#^#" & "-" & "^#^#" & "-" & "^#^#"
    End With
    
    Selection.Find.Execute
    Selection.Copy

End Sub

Open in new window

0
 
Andreas HermleTeam leaderAuthor Commented:
Dear royhsia:

thank you very much for your kind help. I am afraid to tell you that your kind is not producing the desired result. I will award all the points to Graham. Nevertheless, thank you very much for the time taken and your professional efforts.

Regards, Andreas
0
 
Andreas HermleTeam leaderAuthor Commented:
Graham,

as always, your codes work like a charm.

Thank you very much for your great and professional support.

Regards, Andreas
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now