Results 1 to 4 of 4

Thread: [RESOLVED] Function to Check String Format and Replace with a Different Format

  1. #1

    Thread Starter
    Addicted Member charmedcharmer's Avatar
    Join Date
    Sep 2003
    Posts
    211

    Resolved [RESOLVED] Function to Check String Format and Replace with a Different Format

    Hi guys,

    I already search everywhere but I can't really find anything regarding this problem.

    Let us say I have several strings in my word application in this format "11:59:00" (##:##:##). *Note is doesn't necessarily mean that there 'time'. It's just that they are written in that format.

    What I what is to replace all instances of that string format into "11.59.00" (##.##.##). How do I do that?

    I can see that I can work this through using the find functions wild cards of do it on vb code. But I really can't find any material that will help me on this matter. Thank you in advance.
    C++ Programming is overwhelming.

    Dont let it overwhelm you or you'll fall into the oblivion of its perfection

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: Function to Check String Format and Replace with a Different Format

    You should just be able to use the vb Replace function, try this

    vb Code:
    1. Sub test()
    2.  
    3. Dim aString As String
    4.  
    5. testString = "11:59:00"
    6.  
    7. testString = Replace(testString, ":", ".")
    8.  
    9. Msgbox testString
    10.  
    11. End Sub
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Function to Check String Format and Replace with a Different Format

    Try this

    Code:
    Sub repwords()
    Dim r As Range, i As Integer
    Dim StringOld As String, StringNew As String
    
    Set r = ActiveDocument.Range
    For Each r In ActiveDocument.StoryRanges
        For i = 1 To Len(r.Text)
            If Mid(r.Text, i, 1) = ":" And Mid(r.Text, i + 3, 1) = ":" Then
                StringOld = Mid(r.Text, i - 2, 8)
                StringNew = Replace(StringOld, ":", ".")
                
                Selection.Find.ClearFormatting
                Selection.Find.Replacement.ClearFormatting
                With Selection.Find
                    .Text = StringOld
                    .Replacement.Text = StringNew
                    .Forward = True
                    .Wrap = wdFindContinue
                    .Format = False
                    .MatchCase = False
                    .MatchWholeWord = False
                    .MatchWildcards = False
                    .MatchSoundsLike = False
                    .MatchAllWordForms = False
                End With
                
                Selection.Find.Execute Replace:=wdReplaceAll
            End If
        Next i
    Next r
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4

    Thread Starter
    Addicted Member charmedcharmer's Avatar
    Join Date
    Sep 2003
    Posts
    211

    Re: Function to Check String Format and Replace with a Different Format

    Thank you very much. I got it.
    C++ Programming is overwhelming.

    Dont let it overwhelm you or you'll fall into the oblivion of its perfection

Posting Permissions

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



Click Here to Expand Forum to Full Width