Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-28-2022, 04:25 PM
jeffreybrown jeffreybrown is offline VBA to find and replace but ask to continue Windows 10 VBA to find and replace but ask to continue Office 2016
Expert
VBA to find and replace but ask to continue
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default VBA to find and replace but ask to continue

I currently use this code to Subscript certain areas of my document, but it has been updating areas I don't need subscipted. How can I have the macro ask me if that paragraph should have the numbers subscripted?

Code:
Sub SubscriptPara()
    Selection.HomeKey Unit:=wdStory, Extend:=wdMove
    
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        
        With .Replacement.Font
            .Bold = True
            .Color = vbBlack
            .Superscript = True
            .Size = 10
        End With
        
        .Text = "[0-9]{1,} "
        .Replacement.Text = "^&"
        .Execute Replace:=wdReplaceAll

        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchWildcards = True
    End With
End Sub

Reply With Quote
  #2  
Old 06-29-2022, 12:12 AM
mark99k's Avatar
mark99k mark99k is offline VBA to find and replace but ask to continue Windows 7 32bit VBA to find and replace but ask to continue Office 2010 32bit
Novice
 
Join Date: Oct 2012
Location: California USA
Posts: 20
mark99k is on a distinguished road
Default

This'll prompt for, and act on, each occurrence. If you actually do want your response to apply to all occurrences in the paragraph (seems unlikely but that's how I read your question), that'll need some more work.

Code:
Sub SubscriptPara()
    Selection.HomeKey wdStory 
    
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[0-9]{1,} "
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchWildcards = True
        Do While .Execute
            If MsgBox("Subscript here?", vbYesNo) = vbYes Then
                With Selection.Range.Font
                    .Bold = True
                    .Color = vbBlack
                    .Superscript = True
                    .Size = 10
                End With
            End If
        Loop

    End With
End Sub
Reply With Quote
  #3  
Old 06-29-2022, 12:19 AM
Bikram Bikram is offline VBA to find and replace but ask to continue Windows 10 VBA to find and replace but ask to continue Office 2007
Advanced Beginner
 
Join Date: Jul 2021
Location: Nepal
Posts: 90
Bikram is on a distinguished road
Default

Try:
Code:
Sub SubscriptPara()
    Selection.HomeKey Unit:=wdStory, Extend:=wdMove
    Dim a As String
    With Selection.find
        .ClearFormatting
        .Replacement.ClearFormatting
        With .Replacement.font
            .Bold = True
            .Color = vbBlack
            .Superscript = True
            .Size = 10
        End With
        
        .Text = "[0-9]{1,} "
        .Replacement.Text = "^&"
        .Forward = True
        .Wrap = wdFindContinue
        .format = True
        .MatchWildcards = True
    Do While .Execute = True
        a = MsgBox("Do you want the selection to be subscriptted?", vbYesNoCancel)
        If a = vbYes Then
            .Execute Replace:=wdReplaceOne
        ElseIf a = vbNo Then
            .Execute
        Else
            Exit Sub
        End If
    Loop
    End With
End Sub
Reply With Quote
  #4  
Old 06-29-2022, 05:48 AM
gmaxey gmaxey is offline VBA to find and replace but ask to continue Windows 10 VBA to find and replace but ask to continue Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Not very elegant, but seems to work:


Code:
Sub FindNum()
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .ClearFormatting
    .Font.Superscript = False 'Don't find if already superscripted
    .Text = "[0-9]{1,} "
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchWildcards = True
    Do While .Execute
      oRng.Select
      Select Case MsgBox("Do you want the numbers found in the current paragraph to be subscripted?", vbYesNoCancel)
        Case vbYes
          SubscriptPara oRng.Paragraphs(1).Range
         Case vbNo
          oRng.End = oRng.Paragraphs(1).Range.End
        Case Else: Exit Sub
      End Select
      oRng.Collapse wdCollapseEnd
    Loop
  End With
lbl_Exit:
  Exit Sub
End Sub
Sub SubscriptPara(oRng As Range)
Dim oRngPar As Range
  Set oRngPar = oRng.Duplicate
  With oRng.Find
    .ClearFormatting
    .Font.Superscript = False 'Don't find if already superscripted
    .Text = "[0-9]{1,} "
    .MatchWildcards = True
     Do While .Execute
      If oRng.InRange(oRngPar) Then
        With oRng.Font
          .Bold = True
          .Color = vbBlack
          .Superscript = True
          .Size = 10
        End With
      Else
        Exit Do
      End If
      oRng.Collapse wdCollapseEnd
    Loop
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/

Last edited by gmaxey; 06-29-2022 at 12:20 PM. Reason: Correct code to OP specs
Reply With Quote
  #5  
Old 06-29-2022, 11:53 AM
jeffreybrown jeffreybrown is offline VBA to find and replace but ask to continue Windows 10 VBA to find and replace but ask to continue Office 2016
Expert
VBA to find and replace but ask to continue
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Thank you to all for the responses.

Quote:
If you actually do want your response to apply to all occurrences in the paragraph (seems unlikely but that's how I read your question), that'll need some more work.
I like what it is doing now, but would it be possible to have the first number selected but have all the numbers within the paragraph subscripted.

Here is an example:

Quote:
23 "If anyone has ears to hear, let him hear." 24 And He was saying to them, "Take care what you listen to. By your standard of measure it will be measured to you; and more will be given you besides. 25 "For whoever has, to him more shall be given; and whoever does not have, even what he has shall be taken away from him."
In this paragraph there are three numbers, and I would want all of them subscripted. With the options provided, I need to select "Yes" three times which is not a big deal, but if I could select "Yes" one time and all numbers in the paragraph are subscripted, that would help tremendously. This of course would still loop thru the entire document.

And BTW, Greg I'm keen to your code since it's using the range object. Also Greg, thank you for your service. I myself have done some USAF time.
Reply With Quote
  #6  
Old 06-29-2022, 12:21 PM
gmaxey gmaxey is offline VBA to find and replace but ask to continue Windows 10 VBA to find and replace but ask to continue Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Jeffrey, see my edited post
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 06-29-2022, 12:26 PM
jeffreybrown jeffreybrown is offline VBA to find and replace but ask to continue Windows 10 VBA to find and replace but ask to continue Office 2016
Expert
VBA to find and replace but ask to continue
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Wow. That is absolutely perfect Greg. Thank you very much.

In your code, I see you use the lbl_Exit: a lot.

Can you explain why you used that?
Reply With Quote
  #8  
Old 06-29-2022, 12:31 PM
mark99k's Avatar
mark99k mark99k is offline VBA to find and replace but ask to continue Windows 10 VBA to find and replace but ask to continue Office 2016
Novice
 
Join Date: Oct 2012
Location: California USA
Posts: 20
mark99k is on a distinguished road
Default

Jeffrey, just FYI: I noticed that although your post is about subscripts, your original code creates superscripts.
Reply With Quote
  #9  
Old 06-29-2022, 12:37 PM
jeffreybrown jeffreybrown is offline VBA to find and replace but ask to continue Windows 10 VBA to find and replace but ask to continue Office 2016
Expert
VBA to find and replace but ask to continue
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Yes, thanks mark99k, I see that now. I surely want to do superscripts.
Reply With Quote
  #10  
Old 06-29-2022, 01:26 PM
gmaxey gmaxey is offline VBA to find and replace but ask to continue Windows 10 VBA to find and replace but ask to continue Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Jeffery,


A personal style thing. Not always needed.


Code:
Sub Demo()
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  On Error GoTo Err_Handler
  Err.Raise 6
  MsgBox "You won't see this message"
lbl_Exit:
  Set oRng = Nothing
  Exit Sub
Err_Handler:
  MsgBox "An error has occurred"
  Resume lbl_Exit
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #11  
Old 06-29-2022, 01:28 PM
jeffreybrown jeffreybrown is offline VBA to find and replace but ask to continue Windows 10 VBA to find and replace but ask to continue Office 2016
Expert
VBA to find and replace but ask to continue
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Got it, thanks again Greg.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you use the find and replace tool to find dates and times in Excel 2013? Jules90 Excel 3 04-14-2020 07:40 PM
In Find and Replace, can Word stop after each Replace? wardw Word 1 06-08-2017 02:47 PM
VBA to find and replace but ask to continue Find what box in Find and replace limits the length of a search term Hoxton118 Word VBA 7 06-10-2014 05:05 AM
VBA to find and replace but ask to continue Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM
VBA to find and replace but ask to continue Help with find and replace or query and replace shabbaranks Excel 4 03-19-2011 08:38 AM

Other Forums: Access Forums

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