Solved

VBA Excel replace strings in active cells

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

Independent Software Vendors: 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 will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

688 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