2

I have two strings like

 str1= "[abc 1],[def 2],[ghi 3],[jkl 4],[mno 5]"
 str2="[def 2],[mno 5]"

The strings in str2 [def 2],[mno 5] should be deleted or replaced with "" in str1. result will be

str1="[abc 1],[ghi 3],[jkl 4]"

I tried replace function but not working giving full string str1

strorg1 = Replace(str1, str2,"")
1
  • You could use SPLIT on string2 and iterate through the array. Commented Sep 8, 2016 at 15:35

3 Answers 3

2

Try this (sorry for not being able to format as code by now)

Option Explicit

Sub Main()
    Dim str1 As String, str2 As String
    Dim str As Variant

    str1 = "[abc 1],[def 2],[ghi 3],[jkl 4],[mno 5]"
    str2 = "[def 2],[mno 5]"

    str1 = "|" & str1 & "|"
    For Each str In Split(str2, ",")
        str1 = Replace(str1, str, "")
    Next str
    str1 = Replace(Replace(Replace(str1, ",,", ","), "|,", ""), ",|", "")
    MsgBox str1
End Sub     
Sign up to request clarification or add additional context in comments.

3 Comments

please may i know why did u add "|" for str1
For marking the beginning and end of it and easily remove possible trailing commas. Try without! Did it work for you?
Glad to be of help. Good coding!
2

This works i think:

Option Explicit
Sub gen()
    Dim ReplaceList(1 To 5) As String
    Dim str1 As String, strToReplace As Variant
    Dim a() As String
    Dim element As Long

    str1 = "[abc 1],[def 2],[ghi 3],[jkl 4],[mno 5]"
    ReplaceList(1) = "[def 2]"
    ReplaceList(2) = "[mno 5]"

    a = Split(str1, ",")

    For element = UBound(a) To 0 Step -1
        For Each strToReplace In ReplaceList

            If a(element) = strToReplace Then
                a(element) = ""
            End If
        Next
    Next
    str1 = Join(a)
    Debug.Print str1
End Sub

edit, i don't have access to Access, I hope this works, if not it should set you on the right track.

Comments

1

Try this one :

Sub Macro1()

Dim str1 As String
Dim str2 As String
Dim strTemp As String
Dim strTemp2 As String
Dim strOut As String
str1 = "[abc 1],[def 2],[ghi 3],[jkl 4],[mno 5]"
str2 = "[def 2],[mno 5]"
strOut = str1
Do
    strTemp = Application.WorksheetFunction.Search("]", str2)
    strTemp2 = Mid(str2, 1, strTemp + 1)
    strOut = Replace(strOut, strTemp2, "")
    str2 = Replace(str2, strTemp2, "")
Loop Until str2 = ""
End Sub     

It will parse your str2, cut pieces inside [] and remove it from str1 one by one.

The output is what you expected, using excel 2010.

3 Comments

Range("A2").Select and Application.WorksheetFunction.Search("]", str2) not access vba functions
then state explicitly you want an access vba solution. you can remove the range.Select . I let you search an equivalent of the search function, since it won't be complicated to find.
"then state explicitly you want an access vba solution" - look at the question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.