Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-08-2024, 01:42 AM
yacov yacov is offline clean extra space Windows 10 clean extra space Office 2016
Competent Performer
clean extra space
 
Join Date: Oct 2019
Posts: 139
yacov is on a distinguished road
Default clean extra space

Hello,
I tried to change the macro below so that in one run it will delete an unnecessary space next to the + - signs
Is it possible?
I can duplicate the macro and put one mark per macro but that leaves me with a lot of shortcuts.
Thanks

Sub MFindReplacePARA()
Dim aDoc As Document, strFindText As String, strReplaceText As String
strFindText = "^p^p"
strReplaceText = "^p"
strFindText = " +"
strReplaceText = "+"
strFindText = "+ "
strReplaceText = "+"
strFindText = "- "


strReplaceText = "-"
strFindText = " -"
strReplaceText = "-"
For Each aDoc In Application.Documents
With aDoc.Range.Find
.Text = strFindText
.Replacement.Text = strReplaceText
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
Next aDoc
End Sub
Reply With Quote
  #2  
Old 02-08-2024, 08:08 AM
vivka vivka is offline clean extra space Windows 7 64bit clean extra space Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, Yacov! It's not difficult:
Code:
Sub MFindReplacePARA()

        With selection.range.Find
            .text = " {1,}([+-]) {1,}"
            .Replacement.text = " \1 "
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute Replace:=wdReplaceAll
        End With
 End Sub
If there's an error, replace ',' with ';' in curly brackets.

You may modify the code to meet your requirements.
Reply With Quote
  #3  
Old 02-08-2024, 09:06 AM
yacov yacov is offline clean extra space Windows 10 clean extra space Office 2016
Competent Performer
clean extra space
 
Join Date: Oct 2019
Posts: 139
yacov is on a distinguished road
Default

thanks a lot Vivka.
For some reason it sometimes works for me and sometimes it doesn't.

But I recorded a macro that runs several macros and each one does a swap. And so by a single shortcut I can run them all together.

Sub CLEAN()
'
' CLEAN Macro
'
'
Application.Run MacroName:="Normal.NewMacros.MFindReplaceMINUS1"
Application.Run MacroName:="Normal.NewMacros.MFindReplaceMINUS2"
Application.Run MacroName:="Normal.NewMacros.MFindReplacePLUS1"
Application.Run MacroName:="Normal.NewMacros.MFindReplacePLUS2"
Application.Run MacroName:="Normal.NewMacros.MFindReplacePARA"
Application.Run MacroName:="Normal.NewMacros.MFindReplace"
End Sub
Reply With Quote
  #4  
Old 02-08-2024, 09:15 AM
vivka vivka is offline clean extra space Windows 7 64bit clean extra space Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Yacov, could you, please, post a sample of the problem document. I'm curious, because for me the code works smoothly: it leaves only one space before and after + and -. Maybe you want to delete single spaces also? Then use:
Code:
Sub MFindReplacePARA()

        With selection.range.Find
            .text = " {1,}([+-]) {1,}"
            .Replacement.text = "\1"
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchWildcards = True
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute Replace:=wdReplaceAll
        End With
 End Sub
Reply With Quote
  #5  
Old 02-08-2024, 09:32 AM
yacov yacov is offline clean extra space Windows 10 clean extra space Office 2016
Competent Performer
clean extra space
 
Join Date: Oct 2019
Posts: 139
yacov is on a distinguished road
Default

Thank a lot, now it works great.

Sorry for not explaining myself in the first place.
Reply With Quote
  #6  
Old 02-08-2024, 09:36 AM
vivka vivka is offline clean extra space Windows 7 64bit clean extra space Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Yacov, the following code will also delete empty paragraps:
Code:
Sub Spaces()

    With selection.range.Find
        .text = Chr(13) & "{2,}"
        .Replacement.text = Chr(13)
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
        .text = " {1,}([+-]) {1,}"
        .Replacement.text = "\1"
        .Execute Replace:=wdReplaceAll
    End With
End Sub
Reply With Quote
  #7  
Old 02-08-2024, 10:01 AM
yacov yacov is offline clean extra space Windows 10 clean extra space Office 2016
Competent Performer
clean extra space
 
Join Date: Oct 2019
Posts: 139
yacov is on a distinguished road
Default

very useful,
Thank you very much
Reply With Quote
  #8  
Old 02-08-2024, 10:27 AM
vivka vivka is offline clean extra space Windows 7 64bit clean extra space Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

You are welcome, Yacov!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing extra space between word BillMcEnaney Word 9 02-16-2023 11:03 PM
How to delete extra space (between lines)? Gerald Wilhelm Word 15 08-26-2020 04:08 AM
clean extra space Merge field needs extra space when it populates jaj42305 Mail Merge 3 04-21-2016 03:33 PM
extra space before equation Nice2007 Word 1 04-27-2012 05:05 PM
clean extra space Slight extra space between lines. lucadelcarlo Word 1 04-23-2011 04:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:55 PM.


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