Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB6 string replace

Posted on 2014-12-02
12
Medium Priority
?
336 Views
Last Modified: 2015-01-28
So I have an issue that I cannot figure out.

I have a string that contains email addresses

I.E
test@test.com, test@123.com, test@456.com

Open in new window


I have another string set of
 I.E
test@123.com, test@test.com

Open in new window


I want to remove the second string set from string 1. Even though they are not in order.  I don't know how to figure this one out.  I would assume that i need to separate the second string into an array perhaps and do a foreach loop which removes them from string one.

Scratching my head at this moment.
0
Comment
Question by:desiredforsome
  • 6
  • 5
12 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40476271
what about:
Dim s1 as String 
Dim s2 as String 
s1 =  "test@test.com, test@123.com, test@456.com"
s2 = "test@123.com, test@test.com"

Dim arr() As String = Split(s2, ",")
Dim i as Long
s1 = replace(s1, " ", "")
i = lbound(arr)
while i <= ubound(arr)
begin
   s1 = replace(s1, arr(i) & ",", "")
   i = i + 1
end

Open in new window

0
 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40476308
This may help.

Sub Remove()

    Const delimiter As String = ","

    Dim iterStr As Variant
    Dim mainlist As String
    Dim sublist As String
    Dim split_sublist() As String

    mainlist = "test@test.com, test@123.com, test@456.com"
    sublist = "test@123.com, test@test.com"
    
    split_sublist = Split(sublist, delimiter)
    
    For Each iterStr In split_sublist

        'clean iterStr the way you want.
        iterStr = Replace(iterStr, " ", "")

        mainlist = Replace(mainlist, iterStr & delimiter, "")
        mainlist = Replace(mainlist, iterStr, "")

    Next iterStr
    
    'clean mainlist the way you want.
    mainlist = Trim(mainlist)

End Sub

Open in new window

0
 

Author Comment

by:desiredforsome
ID: 40476804
For Hakan, I get a Mismatch on the FOr each line

For Guy -

I dont see where this outputs to a string. Only an array.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:desiredforsome
ID: 40476808
Also get unexptected text on Guy.
0
 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40476819
Can you please write me what error is? I don't have vb6 installed. I try it in excel VBA.
Also please change "Dim iterStr As Variant" to "Dim iterStr" and try.
0
 

Author Comment

by:desiredforsome
ID: 40476824
So the whole error is Error: 10080 (10080) Type Mismatch Line number 17. This is also after I have changed the dimension.

It is on the "For Each" line of code.
0
 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40476833
Can you please now change it to "Dim iterStr As String" and try?
0
 

Author Comment

by:desiredforsome
ID: 40476845
I get Error 0 Var is already defined as a different type line 17 (that is on the For Each line)
0
 
LVL 5

Expert Comment

by:Hakan Yılmaz
ID: 40476856
Please try this.

Sub Remove()

    Const delimiter As String = ","

    Dim mainlist As String
    Dim sublist As String
    Dim split_sublist() As String

    mainlist = "test@test.com, test@123.com, test@456.com"
    sublist = "test@123.com, test@test.com"
    
    split_sublist = Split(sublist, delimiter)
    
    For Each iterStr as String In split_sublist

        'clean iterStr the way you want.
        iterStr = Replace(iterStr, " ", "")

        mainlist = Replace(mainlist, iterStr & delimiter, "")
        mainlist = Replace(mainlist, iterStr, "")

    Next iterStr
    
    'clean mainlist the way you want.
    mainlist = Trim(mainlist)

End Sub

Open in new window

0
 

Author Comment

by:desiredforsome
ID: 40476861
Error 0
Expeting 'In' line number 16   (Same line as always). I am try to figure this out with research and and pulling hair out.
0
 
LVL 5

Accepted Solution

by:
Hakan Yılmaz earned 2000 total points
ID: 40476881
VBA and VB are very similiar, i have mixed syntax.

Changed code to loop between bounds. Please try this.

Sub Remove()

    Const delimiter As String = ","

    Dim i As Integer
    Dim iterStr As String
    Dim mainlist As String
    Dim sublist As String
    Dim split_sublist() As String

    mainlist = "test@test.com, test@123.com, test@456.com"
    sublist = "test@123.com, test@test.com"
    
    split_sublist = Split(sublist, delimiter)
    
    For i = LBound(split_sublist) To UBound(split_sublist)

        iterStr = split_sublist(i)

        'clean iterStr the way you want.
        iterStr = Replace(iterStr, " ", "")

        mainlist = Replace(mainlist, iterStr & delimiter, "")
        mainlist = Replace(mainlist, iterStr, "")

    Next i
    
    'clean mainlist the way you want.
    mainlist = Trim(mainlist)

End Sub

Open in new window

0
 

Author Closing Comment

by:desiredforsome
ID: 40476894
OMG AWESOME!!!!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question