Consulting

Results 1 to 13 of 13

Thread: How to remove [TAG 001], [TAG 002], [TAG 003], etc from string

  1. #1

    How to remove [TAG 001], [TAG 002], [TAG 003], etc from string

    I need to remove all instances of [TAG 001], [TAG 002], [TAG 003], etc. from a string. How do I do that using VBA?

  2. #2
    Maybe smoething like:

    Function RemoveTAGs(InputStr As String) As String
    Dim i As Long
    Dim tempStr As String
    tempStr = InputStr
    For i = 1 To 9
        tempStr = Replace(tempStr, "[TAG 00" & i & "]", "")
    Next i
        RemoveTAGs = tempStr
    End Function
    A
    B
    1
    The quick [TAG 001] brown fox [TAG 002] jumps [TAG 003] over the [TAG 004] lazy [TAG 005] dog The quick brown fox jumps over the lazy dog

    A
    B
    1
    The quick [TAG 001] brown fox [TAG 002] jumps [TAG 003] over the [TAG 004] lazy [TAG 005] dog =RemoveTAGs(A1)
    Sheet: Sheet1

  3. #3
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Hello!
    Maybe a wildcard would be better.
    sub test
      dim s$
      s = "The quick [TAG 001] brown fox [TAG 002] jumps [TAG 003] over the [TAG 004] lazy [TAG 005] dog"
      debug.print replace(s, "[TAG 00?]", "")
    end sub

  4. #4
    What if it iterates to 10 or more, e.g. [TAG 010], [TAG 011] etc.?

  5. #5
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Here's a guess... use Wolfies code but with [TAG 0??] and if you are going into the hundreds [TAG ???]
    Semper in excretia sumus; solum profundum variat.

  6. #6
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Hi paulked!
    Thank you for explaining for me.
    Yes, I think so, too.

  7. #7
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    Semper in excretia sumus; solum profundum variat.

  8. #8
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Sorry to swaggerbox, KOKOSEK and paulked
    When I was taking a shower, I realized that I had made a low-level mistake.
    Wildcards are not supported for the replace() function but the range.replace method.
    So, If the string is in column a, it can be replaced directly like below
    sub test
      columns("a").replace "[TAG 0??]", ""
    end sub
    But if want to process strings, it need to use other methods.
    KOKOSEK's method is OK, and can also use the regExp method.

  9. #9
    Question is: is TAG 010 can exist without TAG 009, 008, 007 etc.
    If not we can count how many TAGs we've got then create loop from 1 to 'TAG.Count'.
    For ex.

    The quick [TAG 001] brown fox [TAG 002] jumps [TAG 003] over the [TAG 004] lazy [TAG 005] dog
    5 TAGs so:

    For i=1 to 5
    and also what if >9 TAGs.

    OR

    use other way (regExp is probably best method).
    I am not strong enough with regExp.

    not so bad. With regExp:

    Function RemoveTAGs(InputStr As String) As String
    Dim i As Long
    Dim tempStr As String
    Dim regTag As Object
    Set regTag = CreateObject("VBScript.RegExp")
     tempStr = InputStr
     regTag.Global = True
     regTag.Pattern = "\[TAG\s[0-9]{3}\]\s"
     RemoveTAGs = regTag.Replace(tempStr, "")
    End Function
    should works (of course due to {3} parameter from TAG 001 to TAG 999)
    Last edited by KOKOSEK; 11-14-2019 at 07:26 AM. Reason: regExp added

  10. #10
    VBAX Master paulked's Avatar
    Joined
    Apr 2006
    Posts
    1,007
    Location
    And another way, relying on square brackets only being used in [TAG ???]!
    Sub ked()
        Dim str As String, arr As Variant, newstr As String, i As Long
        str = "The quick [TAG 001] brown fox [TAG 002] jumps [TAG 003] over the [TAG 004] lazy [TAG 005] dog"
        arr = Split(str, " ")
        For i = 0 To UBound(arr)
            If Not Left(arr(i), 4) = "[TAG" And Not Right(arr(i), 1) = "]" Then newstr = newstr & arr(i) & " "
        Next
        newstr = Left(newstr, (Len(newstr) - 1))
        MsgBox newstr
    End Sub
    Semper in excretia sumus; solum profundum variat.

  11. #11
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    If the number is large (for example, 1-100000), it is more efficient to use regularization than for...next.
    If it is to remove [TAG ???] directly in the worksheet, I still insist that it is easier to use range.replace method + wildcard.
    Here is a regular UDF example:
    Function regReplace(s$)
      Dim reg As Object
      Set reg = CreateObject("vbscript.regexp")
      With reg
        .Pattern = "\[TAG \d+\]"
        .Global = True
        regReplace = Application.Trim(.Replace(s, ""))
      End With
    End Function

  12. #12
    Thanks a lot guys. Plenty of help.

  13. #13
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    Why didn't you post a representative example ?
    Now I see see only guesses.

Posting Permissions

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