Solved

Replace multiple words within a string

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

821 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