Solved

Find and Replace with Excel VBA

Posted on 2015-02-06
3
83 Views
Last Modified: 2016-02-10
Cells.Replace What:="1/2/1900", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

Is it possible to modify the above VBA code to look for special range rather than the whole worksheet ?

Tks
0
Comment
Question by:AXISHK
3 Comments
 
LVL 59

Accepted Solution

by:
Saurabh Singh Teotia earned 300 total points
ID: 40593141
Yes you can do so by...

Lets say i want to check a1 to a10

range("A1:A10").Replace What:="1/2/1900", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

Open in new window


In the first command cells means every cell in your worksheet.

Saurabh...
0
 
LVL 29

Assisted Solution

by:gowflow
gowflow earned 200 total points
ID: 40593175
correct and more

you have there Lookat:=xlPart
You have to be careful as you are searching for a date not a string which is something specific (unless this date comes in a string like: I went to the store on 1/2/1900 and got banned ..) but don't think so as this 1/2/1900 is more like a no date that shows as a date

So I would change the Lookat:=xlPart
by
Lookat:=xlwhole

and more but following does not apply to Replace but rather apply to Find but it is good mentioning as we are talking about the subject here.,

when you search for dates usually excel have a problem as the date you see is only a format of the real date that is a serial number so you may end up most of the time looking for a date with this instruction and not finding it although it is obvious for you that the date is there. All this to say that when you look for a date you are better off specifying in the argument Lookin xlformulas and not xlvalues as one would do in values.

So to sum it up for your instruction it would be safer to have it as follows for a range of say A1:A10
range("A1:A10").Replace What:="1/2/1900", Replacement:="", LookAt:=xlWhole, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
0
 

Author Closing Comment

by:AXISHK
ID: 40607134
Tks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now