Solved

Find and Replace with Excel VBA

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

708 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

13 Experts available now in Live!

Get 1:1 Help Now