+ Reply to Thread
Results 1 to 9 of 9

How to make VBA loop until false

Hybrid View

  1. #1
    Registered User
    Join Date
    02-21-2024
    Location
    Middx UK
    MS-Off Ver
    2003
    Posts
    31

    How to make VBA loop until false

    My macro identifies a pattern of Digit Space Digit in my document and then carries out certain actions :

    Selection.Find.ClearFormatting
        With Selection.Find
            .Text = "^# ^#"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.MoveLeft Unit:=wdCharacter, Count:=2
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        Selection.TypeParagraph
    It works fine , but stops after one iteration. I'd like it to repeat through the document rather than stop after the first occasion the search pattern is found , and go though until all matches have been treated.

    Can someone suggest a modification for the code so that it loops through the document until all of the Digit Space Digit searches are found?


    Thanks for any advice.
    Last edited by CaMeSuffit; 04-29-2024 at 04:58 PM.
    Currently using Access , Word and Excel 2003

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,033

    Re: How to make VBA loop until false

    Sorry _ I misread your post. Just use a goto and a check for not finding what you are replacing to get out of the loop.....

        Selection.Find.ClearFormatting
    FindNextNumbers:
        With Selection.Find
            .Text = "^# ^#"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        If Selection.Find.Execute = False Then Exit Sub
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.MoveLeft Unit:=wdCharacter, Count:=2
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        Selection.TypeParagraph
        GoTo FindNextNumbers
    Last edited by Bernie Deitrick; 04-29-2024 at 04:02 PM.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    02-21-2024
    Location
    Middx UK
    MS-Off Ver
    2003
    Posts
    31

    Re: How to make VBA loop until false

    Bernie - thanks

    Unfortunately this isn't a 'Find and Replace' operation. It just Finds the search term and then runs certain tasks on it. For this reason , the macro needs to run right through to the end for each time it finds the search term.

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,033

    Re: How to make VBA loop until false

    Sorry - I misread your post, and have changed my reply to a conditional loop....

        Selection.Find.ClearFormatting
    FindNextNumbers:
        With Selection.Find
            .Text = "^# ^#"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        If Selection.Find.Execute = False Then Exit Sub
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.MoveLeft Unit:=wdCharacter, Count:=2
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        Selection.TypeParagraph
        GoTo FindNextNumbers

  5. #5
    Registered User
    Join Date
    02-21-2024
    Location
    Middx UK
    MS-Off Ver
    2003
    Posts
    31

    Re: How to make VBA loop until false

    Thanks - that does the job perfectly. I'm grateful.

    BTW I'm finding that nothing will run after it. If placed ahead of other VBA within a longer macro then the whole macro stops at

    GoTo FindNextNumbers

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,033

    Re: How to make VBA loop until false

    I had it exit the sub - to continue on, use this:
        Selection.Find.ClearFormatting
    FindNextNumbers:
        With Selection.Find
            .Text = "^# ^#"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        If Selection.Find.Execute = False Then Goto FoundAllNumbers
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.MoveLeft Unit:=wdCharacter, Count:=2
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        Selection.TypeParagraph
        GoTo FindNextNumbers
    
    FoundAllNumbers:
    
        'other code continues here.....

  7. #7
    Registered User
    Join Date
    02-21-2024
    Location
    Middx UK
    MS-Off Ver
    2003
    Posts
    31

    Re: How to make VBA loop until false

    OK ignore my last post. I've amended the section

        If Selection.Find.Execute = False Then Exit Sub
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.MoveLeft Unit:=wdCharacter, Count:=2
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        Selection.TypeParagraph
        GoTo FindNextNumbers
    to

        
       If Selection.Find.Execute = False Then GoTo Jump
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        Selection.MoveLeft Unit:=wdCharacter, Count:=2
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        Selection.TypeParagraph
        GoTo FindNextNumbers
        
    Jump:
    and this fixes the continuation issue. Thanks again

  8. #8
    Registered User
    Join Date
    02-21-2024
    Location
    Middx UK
    MS-Off Ver
    2003
    Posts
    31

    Re: How to make VBA loop until false

    Perfect - Thanks!

  9. #9
    Registered User
    Join Date
    02-21-2024
    Location
    Middx UK
    MS-Off Ver
    2003
    Posts
    31

    Re: How to make VBA loop until false

    All working.
    Last edited by CaMeSuffit; 05-09-2024 at 10:34 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Can I make all TextBoxes on a particular UserForm Visible = False?
    By Michael Island in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-14-2020, 04:59 PM
  2. Activex checkboxes make them all false
    By Phlebas0403 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-13-2017, 02:11 PM
  3. IF false go to next cell and repeat (loop)
    By Tlund1 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-29-2016, 12:33 PM
  4. [SOLVED] Vba code to do stuff when loop returns false
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-24-2015, 01:08 PM
  5. Make optionbuttons false
    By sorendk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-13-2013, 11:57 AM
  6. [SOLVED] Make 0 into a negative and NOT false or true
    By Slender in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 04-09-2013, 02:28 PM
  7. make OR() return 0 or 1 instead of true and false
    By boarders paradise in forum Excel General
    Replies: 16
    Last Post: 10-19-2009, 01:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1