Solved

Replace multiple words within a string

Posted on 2014-02-26
5
124 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 14

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

920 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

16 Experts available now in Live!

Get 1:1 Help Now