?
Solved

VBA Excel replace strings in active cells

Posted on 2008-10-31
5
Medium Priority
?
2,096 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
[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
  • 3
5 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 100 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 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 100 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 93

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 93

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 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