Solved

Replace multiple words within a string

Posted on 2014-02-26
5
123 Views
Last Modified: 2014-03-08
Hi,

I have A1 = Welcome <username> today is <date> and you are using version <version>

Now I want cell b2 to equal sell a1 but replace username date and version with other data, now I know how to use substitute to do this, but some of the data I need ones deep within routines I have, so therefore need a vba version which as a function basically does the same thing.

Thank you
0
Comment
Question by:DemonForce
5 Comments
 
LVL 24

Expert Comment

by:Steve
ID: 39888499
VBA would use REPLACE as you would SUBSTITUTE
0
 
LVL 13

Expert Comment

by:Santosh Gupta
ID: 39888501
.
0
 
LVL 13

Accepted Solution

by:
frankhelk earned 500 total points
ID: 39888505
How about this:
Public Sub substitute(ByRef s As String, ByVal magic As String, ByVal content As String)
    substitute = Replace(s, magic, content)
End Sub

Open in new window

Just call it for every magic to be replaced, i.e.
(...)

Dim str as String
str = Range("A1").Value
str = substitute(str, "<username>", "Joe Miller")
str = substitute(str, "<date>", "2014/02/26")
str = substitute(str, "<version>", "1.0.3.2.0 pre-alpha prototype")

(...)

Open in new window

0
 
LVL 3

Expert Comment

by:Jeremy Tyre
ID: 39888506
If I understand this correctly, you might be able to handle this with REPLACE in excel.

Syntax: REPLACE( old_text, start, number_of_chars, new_text )

http://office.microsoft.com/en-us/excel-help/replace-values-HA104054000.aspx
0
 
LVL 3

Expert Comment

by:Jeremy Tyre
ID: 39909077
@DemonForce  Any updates on this?
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now