Solved

Replace multiple words within a string

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

710 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