+ Reply to Thread
Results 1 to 6 of 6

Find a string by VBA Regular Expression and replace a part of that string

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-29-2011
    Location
    Kolkata, India
    MS-Off Ver
    Excel 2003/2007
    Posts
    182

    Find a string by VBA Regular Expression and replace a part of that string

    Hi All,

    I would like to find ; (semicolon) which does not contain double quote on both side of it in an excel range (say- A1 to A10).
    Then all [any character] ; [any character] should replace with [that character] - [that character].

    Examples:

    a;b ---> a-b
    a; b ---> a- b
    a ;b ---> a -b
    a12c;d25dd ---> a12c-d25dd
    a123;425dd ---> a123-425dd
    a123";"325dd ---> a123";"325dd (No change)
    a123;"325dd ---> a123;"325dd (No change)
    a123";325dd ---> a123";325dd (No change)

    Thanks in advance for your support.

    Regards
    taps

  2. #2
    Forum Contributor
    Join Date
    12-31-2012
    Location
    Jhang, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Find a string by VBA Regular Expression and replace a part of that string

    Why to use just RegEx? another method can't be used?

  3. #3
    Forum Contributor
    Join Date
    12-31-2012
    Location
    Jhang, Pakistan
    MS-Off Ver
    Excel 2010
    Posts
    250

    Re: Find a string by VBA Regular Expression and replace a part of that string

    Sub ReplaceWithRegEx()
        Dim regex As Object
        Set regex = CreateObject("VBScript.RegExp")
        With regex
            .Pattern = "[^""] *(;) *[^""]"
            For i = 1 To 10
            If .test(Cells(i, 1)) Then
                Debug.Print Cells(i, 1).Address
                Cells(i, 1) = Replace(Cells(i, 1), ";", "-")
            End If
            Next
        End With
    End Sub

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Find a string by VBA Regular Expression and replace a part of that string

    perhaps
    Sub replaceText()
       With Range("A1:A10")
          .Replace """;""", "|^|", xlPart
          .Replace """;", "|^", xlPart
          .Replace ";""", "^|", xlPart
          .Replace ";", "-", xlPart
          .Replace "|^|", """;""", xlPart
          .Replace "|^", """;", xlPart
          .Replace "^|", ";""", xlPart
       End With
    End Sub
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Find a string by VBA Regular Expression and replace a part of that string

    Should be
    Sub test()
        Dim r As Range
        With CreateObject("VbScript.RegExp")
            .Global = True
            .Pattern = "([^""]);(?!"")"
            For Each r In Range("a1", Range("a" & Rows.Count).End(xlUp))
                r.Value = .Replace(r.Value, "$1-")
            Next
        End With
    End Sub

  6. #6
    Forum Contributor
    Join Date
    09-29-2011
    Location
    Kolkata, India
    MS-Off Ver
    Excel 2003/2007
    Posts
    182

    Re: Find a string by VBA Regular Expression and replace a part of that string

    Hi hafizimran/JosephP/jindon

    Thanks a ton for your valuable support. All codes provided here is working fine

    Regards
    taps

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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