Solved

VBA Excel replace strings in active cells

Posted on 2008-10-31
5
2,091 Views
Last Modified: 2013-12-26
Hi,

I am writing a VBA macro for Excel where I need to replace a template string with another value.

For example, a cell may contain the following text : "{Username} processed the delivery on {DeliveryDate}."

Any of the active cells in the worksheet may have one or more of the template strings.

The template strings are to be replaced by actual values entered into a userform.

Is there an easy way to do the replacament (eg. "{Username}" with "John") in all the active cells, other than cycling through each cell (ie. using a replace all function would be really good, but I dont know how to do it in VBA).

Any suggestions appreciated

Greg
0
Comment
Question by:gbergsma
  • 3
5 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 25 total points
ID: 22855043
Greg,
Did you try recording a macro using the Edit...Replace menu item? It exposes the method that you can use in VBA. I've generalized it in the snippet below.

Brad


Sub GlobalReplace()
With Selection
    .Replace What:="{Username}", Replacement:="John", LookAt:=xlPart, MatchCase:=False
    .Replace What:="{Delivery Date}", Replacement:="11/25/2008", LookAt:=xlPart, MatchCase:=False
End With
End Sub

Open in new window

0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 25 total points
ID: 22855049
Hello gbergsma,

Try this:

With ActiveSheet.UsedRange
    .Replace What:="{Username}", Replacement:=UserForm1.TextBox1.Value, LookAt:=xlPart, _
        MatchCase:=False
    .Replace What:="{DeliveryDate}", Replacement:=UserForm1.TextBox2.Value, LookAt:=xlPart, _
        MatchCase:=False
End With

Regards,

Patrick
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 22855051
Figures Brad would beat me to it :)
0
 

Author Closing Comment

by:gbergsma
ID: 31512239
Though Brad beat you to it, he missed the "UsedRange", so half points each. Thanks guys
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 22855134
Glad to help, Greg :)
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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;…
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 use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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