Consulting

Results 1 to 6 of 6

Thread: Adjust the Space Between 2 Words To 1 Space - Wildcard Search

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location

    Adjust the Space Between 2 Words To 1 Space - Wildcard Search

    Hi folks,

    Hope all are well this day

    I have been trying to solve this peculiar space problem.
    Through out the document there are some characters a comma followed by X

    Example

    'X - comma X

    I am trying to adjust the space between this and the next word, I don’t know what the next word is

    Example

    'X the cat sat on the mat (7 spaces)


    'X Cup of coffee (5 Spaces)

    I want to make them unifrom so there is only 1 space between them

    'X Love Food(1 Space)

    I have looked all over and, well I'm not sure what to do

    I found this


    https://cybertext.wordpress.com/2012...between-words/

    And I did this

     
     
     Sub SpaceBetweenWords()
     
        Dim oRng As Range
     
       
        Set oRng = ActiveDocument.Range
        With oRng.Find
            .Text = "'X"
            Do While .Execute(Forward:=True) = True
       
           
               
                'oRng.Characters.First = "'X"
                
                'oRng.Characters.Last
       
            
                oRng.Collapse wdCollapseEnd
            Loop
        End With
     
       End Sub

    Can I do a search and replace with wildcards or regexular expressions?

    Thank you for any tips and advice
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Wouldn't this be a macro that replaces 2 or more spaces with just one?

    I don't follow the "X" and the "," part since the examples don't have a comma in them
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location

    Smile

    Hello Paul,

    Oops I meant

    A single quotation mark Followed by X is word1 or the first word

    'X word2 some where
    'X word2 some text

    etc

    How woud I find the spaces?
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    You don't need a macro for that but here one is:

    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oRng As Range
      Set oRng = ActiveDocument.Range
      With oRng.Find
        .Text = "('X) {2,}"
        .MatchWildcards = True
        .Replacement.Text = "\1 "
        .Execute Replace:=wdReplaceAll
      End With
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Hello Greg,
    nice to see you

    yes it did it nicely - but it doesnt like the quotation mark for some reason.
    I tested it with

    #X word 2
    #X word2 again

    The spaces were only 1 so it works.

    Well is suppose its no big deal I can just replace the single quote with a hash sign in this case
    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


  6. #6
    VBAX Mentor
    Joined
    Feb 2016
    Location
    I have lived in many places, I love to Travel
    Posts
    413
    Location
    Thanks Greg and have a good weekend,

    and folks too

    Cheers for your help

    dj

    'Extreme VBA Newbie in progress - one step at a time - like a tortoise's pace'


Posting Permissions

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