Solved

Find and Replace with Excel VBA

Posted on 2015-02-06
3
97 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
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…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

789 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