Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Word > Word VBA

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 12-07-2018, 12:01 AM
ganesang ganesang is offline Windows XP Office 2016
Competent Performer
 
Join Date: Jul 2018
Posts: 103
ganesang is on a distinguished road
Default Need to repeat macro for multiple times

Hi guys

I wanted to repeat/run this macro multiple times at a time.



Code:
Sub get_irf fnf()
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "(\<irf fnf=)(" & ChrW(8220) & _
             "[A-Z 0-9]{1,}" & ChrW(8221) & "/\>)(*)^13"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.Cut
    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "(\<irf fnfr=)(" & ChrW(8220) & _
             "[A-Z 0-9]{1,}" & ChrW(8221) & _
            "/\>)-(\<)irf fnf=(" & ChrW(8220) & _
            "[A-Z 0-9]{1,}" & ChrW(8221) & "/\>)"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute
    Selection.PasteAndFormat (wdFormatOriginalFormatting)
    Selection.HomeKey Unit:=wdStory
End Sub
Can anyone help me out on this?

Thanks
Ganesan. G

Last edited by macropod; 12-07-2018 at 03:17 PM. Reason: Added code tags
Reply With Quote
  #2  
Old 12-07-2018, 01:01 AM
Pecoflyer's Avatar
Pecoflyer Pecoflyer is online now Windows 7 64bit Office 2010 64bit
Moderator
 
Join Date: Nov 2011
Location: Brussels Belgium
Posts: 2,225
Pecoflyer is a glorious beacon of lightPecoflyer is a glorious beacon of lightPecoflyer is a glorious beacon of lightPecoflyer is a glorious beacon of lightPecoflyer is a glorious beacon of light
Default

Please wrap your code with code tags - Thx
__________________
Problem solved ? Let others know by clicking " Thread Tools" then " Mark thread as solved".( This can be undone if need be)
Want to thank for the help received ? Click the scales symbol in the upper right corner of a post from the person you want to thank.
Reply With Quote
  #3  
Old 12-07-2018, 01:05 AM
ganesang ganesang is offline Windows XP Office 2016
Competent Performer
 
Join Date: Jul 2018
Posts: 103
ganesang is on a distinguished road
Default

Code:
Sub get_irf fnf()
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(\<irf fnf=)(" & ChrW(8220) & _
"[A-Z 0-9]{1,}" & ChrW(8221) & "/\>)(*)^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Cut
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "(\<irf fnfr=)(" & ChrW(8220) & _
"[A-Z 0-9]{1,}" & ChrW(8221) & _
"/\>)-(\<)irf fnf=(" & ChrW(8220) & _
"[A-Z 0-9]{1,}" & ChrW(8221) & "/\>)"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute
Selection.PasteAndFormat (wdFormatOriginalFormatting)
Selection.HomeKey Unit:=wdStory
End Sub
Reply With Quote
  #4  
Old 12-07-2018, 07:22 AM
Pecoflyer's Avatar
Pecoflyer Pecoflyer is online now Windows 7 64bit Office 2010 64bit
Moderator
 
Join Date: Nov 2011
Location: Brussels Belgium
Posts: 2,225
Pecoflyer is a glorious beacon of lightPecoflyer is a glorious beacon of lightPecoflyer is a glorious beacon of lightPecoflyer is a glorious beacon of lightPecoflyer is a glorious beacon of light
Default

Ok ( you could have done it in the original post which can be edited up to 24 hrs after the first edit)
__________________
Problem solved ? Let others know by clicking " Thread Tools" then " Mark thread as solved".( This can be undone if need be)
Want to thank for the help received ? Click the scales symbol in the upper right corner of a post from the person you want to thank.
Reply With Quote
  #5  
Old 12-07-2018, 03:26 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,680
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

ganesang: When posting code, please use the code tags and apply them to formatted code. Otherwise, your code loses much of whatever structure it had. See my edit to your original post and compare it with your post #3 in this thread.

As for the question itself, it's not apparent why you'd want to run the same macro multiple times, since the use of '.Wrap = wdFindContinue' means all instances of whatever you're trying to replace should be processed on the first execution. As it is, your code is quite inefficient; you review the code you were provided in http://www.msofficeforums.com/word-v...tml#post134040 for indications as to how your code's efficiency could be improved. Consider, too, http://www.msofficeforums.com/word-v...tween-two.html
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #6  
Old 12-07-2018, 03:31 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 963
Guessed is on a distinguished road
Default

What is the point of running it more than once on the same file?

You can create another macro to run a macro multiple times.
Code:
Sub DeJaVu()
  Dim i as integer
  For i = 1 to 10
    MyMacroName
  Next i
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #7  
Old 12-09-2018, 10:09 PM
ganesang ganesang is offline Windows XP Office 2016
Competent Performer
 
Join Date: Jul 2018
Posts: 103
ganesang is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
ganesang: When posting code, please use the code tags and apply them to formatted code. Otherwise, your code loses much of whatever structure it had. See my edit to your original post and compare it with your post #3 in this thread.

As for the question itself, it's not apparent why you'd want to run the same macro multiple times, since the use of '.Wrap = wdFindContinue' means all instances of whatever you're trying to replace should be processed on the first execution. As it is, your code is quite inefficient; you review the code you were provided in http://www.msofficeforums.com/word-v...tml#post134040 for indications as to how your code's efficiency could be improved. Consider, too, http://www.msofficeforums.com/word-v...tween-two.html
Hi Paul,

Thanks for info about the code formatting. I can use it in future.

Actually, i have recorded the macro which needs to be done

Find the first set of value (using first willd card) from the end of document and cut it and replace at the first set of value (from first finding row using second wildcard) from the table.

I have recorded the macro this way, but if have 40 replacements i have to run this macro 40 times manually.

That's why asking to run macro until first wild card value false.

Please let me know if not clear.

Thanks
Reply With Quote
  #8  
Old 12-09-2018, 10:13 PM
ganesang ganesang is offline Windows XP Office 2016
Competent Performer
 
Join Date: Jul 2018
Posts: 103
ganesang is on a distinguished road
Default

Quote:
Originally Posted by Guessed View Post
What is the point of running it more than once on the same file?

You can create another macro to run a macro multiple times.
Code:
Sub DeJaVu()
  Dim i as integer
  For i = 1 to 10
    MyMacroName
  Next i
End Sub
Hi Guess

Thanks for the reply. Please see my response to why i have run this macro multiple times.
Reply With Quote
  #9  
Old 12-09-2018, 10:16 PM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,680
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Quote:
Originally Posted by ganesang View Post
Actually, i have recorded the macro which needs to be done

Find the first set of value (using first willd card) from the end of document and cut it and replace at the first set of value (from first finding row using second wildcard) from the table.

I have recorded the macro this way, but if have 40 replacements i have to run this macro 40 times manually.
This would be a lot easier to respond to if, as in some of your other threads, you attached a copy of the document concerned to a post so we can see what you're working with; otherwise we lack an appropriate context.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #10  
Old 12-09-2018, 11:14 PM
ganesang ganesang is offline Windows XP Office 2016
Competent Performer
 
Join Date: Jul 2018
Posts: 103
ganesang is on a distinguished road
Default

Hi Paul

Please find the attached which i have run manually for each item.
Attached Files
File Type: docx loop macro.docx (14.8 KB, 3 views)
Reply With Quote
  #11  
Old 12-09-2018, 11:26 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 963
Guessed is on a distinguished road
Default

I also think we need to see the context and reason for what you are trying to achieve. Looking at your code, it appears like it might be rewritten to loop along the following lines.
Code:
Sub get_irf_fnf()
  Dim s1 As String, s2 As String
  s1 = "(\<irf fnf=)(" & ChrW(8220) & "[A-Z 0-9]{1,}" & ChrW(8221) & "/\>)(*)^13"
  s2 = "(\<irf fnfr=)(" & ChrW(8220) & "[A-Z 0-9]{1,}" & ChrW(8221) & "/\>)-(\<)irf fnf=(" & ChrW(8220) & "[A-Z 0-9]{1,}" & ChrW(8221) & "/\>)"
  
  Selection.HomeKey Unit:=wdStory
  With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = s1
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = True
    Do While .Execute = True
      Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
      Selection.Cut
      Selection.HomeKey Unit:=wdStory
      .Text = s2
      .Execute
      Selection.PasteAndFormat (wdFormatOriginalFormatting)
      Selection.HomeKey Unit:=wdStory
      .Text = s1
    Loop
  End With
End Sub
We would need to see a sample document to figure out whether that code would be workable.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #12  
Old 12-10-2018, 12:06 AM
ganesang ganesang is offline Windows XP Office 2016
Competent Performer
 
Join Date: Jul 2018
Posts: 103
ganesang is on a distinguished road
Default

Thanks much!!! Guessed

I think this what i want to workout. Great!!

thanks
Reply With Quote
  #13  
Old 12-10-2018, 12:16 AM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,680
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Having seen what you're trying to do, it's apparent the end result is much different from what you sought in: http://www.msofficeforums.com/word-v...nd-values.html. Rather, it seems that the current problem is a result of your inadequate problem description there; almost exactly the same code could have been used.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
  #14  
Old 12-10-2018, 12:20 AM
ganesang ganesang is offline Windows XP Office 2016
Competent Performer
 
Join Date: Jul 2018
Posts: 103
ganesang is on a distinguished road
Default

Hi Paul

I have split in to the two process which is very easier for me as gmayor said lots of code involved in the previous single process.

But the end result is fine what i expected exactly.

thanks
Reply With Quote
  #15  
Old 12-10-2018, 12:52 AM
macropod's Avatar
macropod macropod is online now Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 18,680
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim StrPfx As String, StrSfx As String, StrMid As String, StrTmp As String
Dim i As Long, j As Long, k As Long
StrPfx = "\<irf fnf=“": StrSfx = "”/\>"
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "(" & StrPfx & "[A-Z]@)[0-9]@" & StrSfx & "-\1[0-9]@" & StrSfx
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    StrTmp = Split(Split(.Text, Replace(StrSfx, "\", ""))(0), "“")(1)
    StrMid = StrTmp
    For i = 1 To Len(StrTmp)
      If Right(StrMid, 1) Like "[0-9]" Then
        StrMid = Left(StrMid, Len(StrMid) - 1)
      End If
    Next
    i = CLng(Replace(StrTmp, StrMid, ""))
    StrTmp = Split(Split(.Text, Replace(StrSfx, "\", ""))(1), "“")(1)
    StrMid = StrTmp
    For j = 1 To Len(StrTmp)
      If Right(StrMid, 1) Like "[0-9]" Then
        StrMid = Left(StrMid, Len(StrMid) - 1)
      End If
    Next
    j = CLng(Replace(StrTmp, StrMid, ""))
    StrTmp = ""
    For k = i To j
      StrTmp = StrTmp & StrPfx & StrMid & k & StrSfx & Chr(11)
    Next
    StrTmp = Replace(StrTmp, "\", "")
    .Text = Left(StrTmp, Len(StrTmp) - 1)
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
With the above code, there is no need for your data outside the table.
__________________
Cheers,
Paul Edstein
[MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mail Merge Repeat Record a certain number of times stacey_e Mail Merge 6 11-19-2018 09:38 PM
Repeat macro until no more fields ChrisJ83 Word VBA 2 01-20-2016 04:55 AM
Print Format - Repeat Text From a Cell on Multiple Pages ChristopherHughes Excel 1 12-07-2014 08:31 PM
How to repeat multiple .jpg's over again in Custom Slide show pbyrescue PowerPoint 1 04-03-2014 12:40 PM
Repeat formula 5 times and repeat? Jenny345 Excel 4 06-14-2013 04:37 PM


All times are GMT -7. The time now is 01:00 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft