Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB6 string replace

Posted on 2014-12-02
12
Medium Priority
?
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

721 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