Solved

VBA Excel replace strings in active cells

Posted on 2008-10-31
5
2,093 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 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

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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…
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…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

726 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